Variables illustration


The current date and time is September 9th, 2010 7:10:AM

Companion Web Site

Larry Ullman has a web site devoted to PHP for the World Wide Web: Second Edition at this location.

It is intended as a supplement /complement to the book and has many helpful resources.

The Book

Variable image

PHP for the World Wide Web: Second Edition written by Larry Ullman

Chapter 6
Control Structures

"Writing conditionals in PHP comes down to identifying True or False situations." p132

Creating the HTML Form

As with the work in the preceding chapters, the ideas we will be learning and applying will need a form to work on/from. So an HTML form will be constructed to gather certain information from users which will then be sent to a PHP page.

The if Conditional

The syntax is quite simple:
if (condition) {
statement(s);
}
It depends on the True or False nature of the condition as to whether the statement is executed or not. The Validation Functions listed on page 120 are what decide whether to return a True or False concerning the data entered. There are many, but these were used in the chapter:

Using Else

if (condition) {
statement(s)
}else {
other _statement(s);
}

When using the if-else, the else statement(s) will be executed when the if condition is not met clearly and cleanly!

More Operators

Comparison:

Here is link to the PHP Manual page with the Comparison Operators

Logical:

Here is a link to the PHP Manual page with the Logical Operators

Besides using Logical Operators to make more complex conditionals, Nesting Conditionals can be used.

When constructing Conditionals:

Using elseif

You can use as many elseifs as you wish as part of one if statement as long as else is the last part of the conditional.

The Switch Conditional

This can save time and clarify an if-elseif-else conditional which has become too big, bulky, elaborate. The switch has only one condition which is the value of its variable. It uses cases and works through each case looking for the one that satifies its condition. It needs a 'break' (placed between each case in the conditional)to stop it once it has found its condition. Without the break, it will go right to the closing curly brace even after it finds what it is looking for.

The For Loop

The use of the forloop is to execute sections of code repeatedly according to a number you designate.

for (initial expression; condition; closing expression){
statement(s);
}

Here is an example for causing numbers from 1 to 10 to be printed to the page:
for ($v = 1; $v <=10; $v++){
echo "$v";
}

The Form

Complete this form to register

First Name:
Last name:
Email Address:

Password:


Confirm Password:

Birthday

Favorite Color:



<?php 
/* My Variables for the study group site */
$book "<u>PHP for the World Wide Web: Second Edition</u>";
$author "Larry Ullman";
$myName "Joanne Johnson";
$pageTitle "PHP Study Group ";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $pageTitle ?></title>

<link href="common/ullman.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/ullmanMenu.css" rel="stylesheet" type="text/css" />
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>

<!--[if IE 7]><link rel="stylesheet" href="common/fieldsetStyleIe.css" 
type="text/css" /><![endif]-->

<link href="common/form6.css" rel="stylesheet" type="text/css" />
</head>

<body class="thrColHybHdr">

<div id="container">
  <div id="header">
    <h1><?php echo $pageTitle ?></h1>
    <p>Presented by <?php echo $myName ?><br />
    Started January, 2009<br />
    </p>
    <br />
  <!-- end #header --></div>
  <div id="sidebar1">
    <div align="center">
      <?php include("includes/nav.php"); ?>
    <img src="images/var.jpg" 
    alt="Variables illustration" width="140" height="373" />
      <br />
      <br />    
      <br />
      
    </div>
    <p>The current date and time is <?php echo date ("F jS, Y g:i:A");?></p>
  <!-- end #sidebar1 --></div>
  <div id="sidebar2">
    <h3>Companion Web Site</h3>
    <p>Larry Ullman has a web site devoted to <?php echo $book ?> at this 
    <a href="http://www.dmcinsights.com/phpvqs2/" target="_blank">location</a>.</p>
    <p>It is intended as a supplement /complement to the book and has many 
    helpful resources.</p>
    <ul>
      <li><a href="http://www.dmcinsights.com/phpvqs2/errata.php">Errata</a></li>
      <li><a href="http://www.dmcinsights.com/phorum/list.php?10" target="_blank">Forum</a></li>
      <li><a href="http://blog.dmcinsights.com/" target="_blank">L. Ullman Blog</a></li>
      <li><a href="http://www.dmcinsights.com/bk_pages/faq.php?i=phpvqs2" target="_parent">FAQ</a></li>
      <li><a href="http://de.php.net/manual/en/manual.php" target="_blank">PHP Manual</a></li>
    </ul>
    <p><img src="images/book.jpg" alt="The Book" width="163" height="203" /></p>
    
    <div align="center"><img src="images/var.jpg" alt="Variable image" width="150" height="400" />
      </div>
  <!-- end #sidebar2 --></div>
  <div id="mainContent">
  <center><h3><?php echo $book ?> <span class="spanTitle" >written by</span> 
  <?php echo $author ?></h3></center>
    <h2 align="center">Chapter 6<br />
      Control Structures</h2>
    <p>&quot;Writing conditionals in PHP comes down to identifying True or False situations.&quot; p132</p>
    <p><strong>Creating the HTML Form</strong></p>
    <p>As with the work in the preceding chapters, the ideas we will be learning and applying will 
    need a form to work on/from. 
    So an HTML form will be constructed to gather certain information from users which will then 
    be sent to a PHP page.</p>
    <p><strong>The if Conditional</strong></p>
    <p>The syntax is quite simple:<br />
    if (condition) {<br />
    statement(s);<br />
    }<br />
    It depends on the True or False nature of the condition as to whether the statement is executed or not. 
    The Validation Functions listed on page 120 are what decide whether to return a True or 
    False concerning the data entered. 
    There are many, but these were used in the chapter:</p>
    <ul>
      <li>empty()function, good for noticing form boxes that were skipped
        <ul>
          <li>checks to see if there is a value entered, which would return False</li>
          <li>if there is 0 or an empty string, returns True</li>
        </ul>
      </li>
      <li>isset()function, almostthe opposite of empty()function
        <ul>
          <li>returns True if a variable has a value(even 0 or an empty string)</li>
          <li>returns False if otherwise (I am still a bit unclear about this idea)</li>
        </ul>
      </li>
      <li>is_numeric()function
        <ul>
          <li>returns True if the variable has valid numbers
            <ul>
              <li>integers, decimals, strings (ifa valid number)</li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    <p><strong>Using Else</strong></p>
    <p>if (condition) {<br />
    statement(s)<br />
    }else {<br />
    other _statement(s);<br />
    }</p>
    <p>When using the if-else, the else statement(s) will be executed when the if condition 
    is not met clearly and cleanly!</p>
    <p><strong>More Operators</strong></p>
    <p>Comparison:</p>
    <ul>
      <li>= means to <em>assign</em> a value to a variable</li>
      <li> == means that a variable <em>is equal</em> to a value</li>
    </ul>
    <p>Here is link to the PHP Manual page with the 
    <a href="http://us2.php.net/operators.comparison" target="_blank">Comparison Operators</a></p>
    <p>Logical:</p>
    <ul>
      <li>Assist in creating more 'logical', apparent constructs</li>
    </ul>
    <p>Here is a link to the PHP Manual page with the 
    <a href="http://us2.php.net/manual/en/language.operators.logical.php" target="_blank">Logical Operators</a></p>
    <p>Besides using Logical Operators to make more complex conditionals, Nesting Conditionals can be used. </p>
    <p>When constructing Conditionals:</p>
    <ul>
      <li>the conditional <strong>must</strong> have a True value</li>
      <li>by using parentheses, rules of precedence can be ignored so that your operators are 
      used in the order that you wish<br />
      </li>
    </ul>
    <p><strong>Using elseif</strong></p>
    <p>You can use as many elseifs as you wish as part of one if statement as long as else is 
    the last part of the conditional.</p>
    <p><strong>The Switch Conditional</strong></p>
    <p>This can save time and clarify an if-elseif-else conditional which has become too big, bulky, elaborate. 
    The switch has only one condition which is the value of its variable. It uses cases and works through each 
    case looking for the one that satifies its condition. It needs a 'break' (placed between each case in the 
    conditional)to stop it once it has found its condition. Without the break, it will go right to the closing 
    curly brace even after it finds what it is looking for.</p>
    <p><strong>The For Loop</strong></p>
    <p>The use of the forloop is to execute sections of code repeatedly according to a number you designate.</p>
    <p>for (initial expression; condition; closing expression){<br />
    statement(s);<br />
    }</p>
    <p>Here is an example for causing numbers from 1 to 10 to be printed to the page:<br />
    for ($v = 1; $v &lt;=10; $v++){<br />
    echo &quot;$v&quot;;<br />
    }</p>
    <p><strong>The Form</strong></p>
     <div id="formDiv">
      <p><strong> Complete this form to register</strong>      </p>
      <form action="handle_reg.php" method="post">
        First Name: <input  type="text" name="first_name" size = "20" />
        <br />
        Last name:  <input type="text" name="last_name" size = "20" />
        <br />
        Email Address: <input type="text" name="email" size = "20" />
        <br />
       <p> Password: <input type="password" name="password" size="20" /></p>
        <br />
      <p> Confirm Password: <input type="password" name="confirm" size="20" /></p>
      <table width="300" cellpadding="1" cellspacing="1">
        <caption>
          Birthday
          </caption>
    
 <tr>
       
          <td width="100"><select name="month" >
            <option value="">Month</option>
            <option value="1">January</option>
            <option value="2">February</option>
            <option value="3">March</option>
            <option value="4">April</option>
            <option value="5">May</option>
            <option value="6">June</option>
            <option value="7">July</option>
            <option value="8">August</option>
            <option value="9">September</option>
            <option value="10">October</option>
            <option value="11">November</option>
            <option value="12">December</option>
          </select></td>
          <td width="100"><select name="day" >
              <option value="">Day</option>
              <?php
         
for ($d 1$d <= 31$d++){
         echo 
"<option value=\"$d\">$d </option>\n";
         }
         
          
?>
          </select></td>
         
          <td width="100">
          <input type="text" name="year" value="yyyy" size="4"/>
</td>
 

 </tr>
      </table>
      <br />
      Favorite Color:
      <select name="color">
      <option value="">Pick One</option>
      <option value="red">Red</option>
      <option value="yellow">Yellow</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
      <option value="pink">Pink</option>
      </select>
      <p><br />
        
        
        <input class="button" type="submit" name="submit" value="Register"/>
        
        
      </p>
      </form>
    </div>
    <p><br class="clearfloat" />
    </p>
  </div>
  <div id="footer">
    <p align="center">&copy;R. Joanne Johnson 2009 </p>
    <p align="center"><a href="http://validator.w3.org/check?uri=referer" 
    target="_blank"><img
        src="http://www.w3.org/Icons/valid-xhtml10"
        alt="Valid XHTML 1.0 Transitional" width="88" height="31" border="0" /></a></p>
  <!-- end #footer --></div>
<!-- end #container --></div>
 
<div id="sourceDiv">
   <?php  
   
/* shows the source code of the page */
   
show_source(basename($_SERVER['PHP_SELF'])); ?>
    
  <?php  
   
/* shows source for nav bar include */
  
show_source("includes/nav.php");?>
</div>


</body>
</html>
<ul id="MenuBar1" class="MenuBarVertical">
          <li><a href="/ullmanBook/chapter1.php">Chapter 1</a></li>
      <li><a href="/ullmanBook/chapter2.php">Chapter 2</a></li>
  <li><a href="/ullmanBook/chapter3.php">Chapter 3</a> </li>
  <li><a href="/ullmanBook/chapter4.php">Chapter 4</a></li>
  <li><a href="/ullmanBook/chapter5.php">Chapter 5</a></li>
  <li><a href="/ullmanBook/chapter6.php">Chapter 6</a></li>
      <li><a href="/ullmanBook/chapter7.php">Chapter 7</a></li>
  <li><a href="#">Chapter 8</a></li>
      <li><a href="#">Chapter 9</a></li>
      <li><a href="#">Chapter 10</a></li>
      <li><a href="#">Chapter 11</a></li>
      <li><a href="#">Chapter 12</a></li>
      <li><a href="#">Chapter 13</a></li>
      <li><a href="/ullmanBook/index.php">Home</a></li>
       <li><a href="http://www.lzydaz.com/phpBB3/index.php" target="_blank">Hello World</a></li>
       <li><a href="http://www.lzydaz.com/studygroup/php/index.php" target="_blank">Bonnie</a></li>
       <li><a href="http://www.amaraland.com/studyGroup/index.php" target="_blank">Kc Ladybug</a></li>
       <li><a href="http://krisse.tuna.fi/phpwww/ch1.php" target="_blank">Krisse</a></li>
       
       
    </ul>