StUdY GuiDe

Reference links

ReLaTeD

Other links

* Notes *

Because variable types are not locked in, they can be changed on the fly:
$total = $price * $quantity;
$total = $total * $shipping;
$total = $total - $discount;


Look-up a function in the PHP manual as www.php.net/function_name
Examples:
www.php.net/round
www.php.net/number_format
www.php.net/stripslashes www.php.net/urlencode

Chapters Four - Five - Six

In a Nutshell

PHP Version: 5.2.8


Main Content

<div id="floatRight">
<div id="mainContentHeader">
<h2 id="four">Chapters <a href="#four" title="Using Numbers">Four</a>
 - <a href="#five" title="Using Strings">Five</a>
 - <a href="#six" title="Control Structures">Six</a></h2>
<p>In a Nutshell</p>
</div><!-- close #mainContentHeader -->

<div id="mainContent">

<div id="left">

<div id="banner">
<img src="images/phpftwww.png" height="521" width="23" alt="" />
</div><!--close #banner-->
</div><!--close #left-->

<div id="right">

<h2 class="first">Chapter Four (exercise one)</h2>
<p>Adding, Subtracting, Multiplying, and Dividing</p>

<p>This exercise performs various calculations based on an e-commerce situation.<br />
It takes $quantity, $price, $tax, $shipping,<br />
and $discount values and generates a $total cost.<br />
This $total value is then divided by the number of $payments<br />
to generate a monthly expense $total value.</p>

<h2 class="first">Chapter Four (exercise two)</h2>
<p>Formatting Numbers</p>
<code>
<?php
highlight_string 
("<?php\n
/* number_format() works like round()\n
with added commas. */\n
\$total = number_format (\$total, 2);\n
\$monthly = number_format (\$monthly, 2);\n
round (\$total, 2);//rounds total to 2 decimal places\n
?>"
)
?>
</code>

<h2 class="first">Chapter Four (exercise three)</h2>
<p>Using Multiple Operators</p>

<p>
<strong>Reference:</strong><br />
$total = $price * $quantity;<br />
$total = $total + $shipping;<br />
$total = $total - $discount;<br /></p>
<p>$taxrate = $tax/100;<br />
$taxrate = $taxrate + 1;
</p>

<p>
<strong>Change the way $total is calculated:</strong><br />
$total = (($price * $quantity) + $shipping) - $discount;</p>
<p><strong>Change the way $taxrate is calculated:</strong><br />
$taxrate = ($tax/100) + 1;
</p>

<h2 class="first">Chapter Four (excercise four)</h2>
<p>Incrementing and Decrementing a Number</p>

<p>
<strong>Reference:</strong><br />
$taxrate = ($tax/100) + 1;<br />
<strong>Change the way $taxrate is incremented:</strong><br />
$taxrate = $tax/100; // Value divided by 100<br />
$taxrate++;// Increment this value by 1<br />
<strong>Example:</strong><br />
$var = 10; // 10<br />
$var++; // 11<br />
$var--; //  Returns to 10
</p>

<form action="handle_calc.php" method="post">
<fieldset>
<p>Fill out this form to calculate the total cost:</p>
<ul>
    <li>Price: <input type="text" name="price" size="5" /></li>
    <li>Quantity: <input type="text" name="quantity" size="5" /></li>
    <li>Discount: <input type="text" name="discount" size="5" /></li>
    <li>Tax: <input type="text" name="tax" size="3" /></li>
    <li>Shipping method: <select name="shipping">
    <option value="5.00">Slow and steady</option>
    <option value="8.95">Put a move on it</option>
    </select></li>
    <li>Number of payments to make: <input type="text" name="payments" size="3" /></li>
    <li><input name="reset" type="reset" value="Reset" class="reset" /></li>
    <li><input type="submit" name="submit" value="Calculate!" /></li>
</ul>
</fieldset>
</form>

<h2 class="first">Chapter Four (exercise five)</h2>
<p>Creating Random Numbers</p>
<?php
/* This script generates 3 random numbers. */

$n1 rand(09);
$n2 rand(09);
$n3 rand(09);
$n4 rand(09);

// Print out the numbers.
print "<p>And last but not least...<br />
The lucky 3 digit Ohio Lottery numbers are:
<strong class=\"red\">$n1$n2$n3</strong></p>"
;
?>
    
<h2 id="five">Chapter Five (exercise one)</h2>
<p>Connecting Strings</p>

<p><strong>Use the period to concatenate strings formally to understand what is occurring in the code:</strong><br />
$s1 = ' Hello'; // One space before<br />
$s2 = ' World '; // One space before and after<br />
$greeting = $s1 . $s2;<br />
<strong>print $greeting;</strong> // Has the same output as<br />
<strong>print $s1$s2;</strong> // Variables side by side<br />
Output: ( Hello World ) // One space between. One space before and after<br />
print $s1 $s2; // Variables separated<br />
Output: ( Hello &nbsp;World ) // Two spaces between. One space before and after.
</p>

<h2>Chapter Five (exercise two)</h2>
<p>Combating Magic Quotes</p>

<p><strong>If Magic Quotes are enabled (mine are) than any quotes entered into a form field
will display the output with backslashes to escape the quotes.</strong></p>
<p>Using the stripslashes() function will remove these escapes after the variable is created
and before the variable is printed.<br />
<strong>Example:</strong><br />
$name = $_POST['name'];<br />
$name = stripslashes ($name);<br />
print $name;<br />
<strong>Same Output Example:</strong><br />
// This example strips the string after the variable has been posted<br />
$name = stripslashes ($_POST['name']);</p>
<p><strong>If Magic Quotes are disabled and you want an output code to escape slashes,
use the addslashes() function.</strong></p>

<h2>Chapter Five (exercise three)</h2>
<p>Encoding and decoding strings.</p>

<p>The urlencode() function encodes and passes the a string variable to the next page.<br />
No need for htmlentities() or strip_tags().</p>
<p>Values are are URL-encoded before being sent and then decoded into the follow-up page.</p>

<h2>Chapter Five (exercise four)</h2>
<p>Replacing parts of a string.</p>

<p>Example:<br />
$posting = str_replace ('forum', 'Hello World Forum', $posting);<br />
Replaces the word (forum) if entered in the posting field with (Hello World Forum).</p>
<p>
Can also be used to remove offensive language and in multiple lines.
</p>

<h2>Chapter Five (exercise five)</h2>
<p>Tokenizing, Searching, and Comparing Strings.</p>

<p>
Example:<br />
Use the strtok() function to create a substring referred to as a token from a larger string.<br />
$title = strtok($recipe, 'submitted');<br />
Using this code I could pull out a string from a recipe and name it $title before it reaches the word 'submitted'.<br />
So, if all recipes as $recipe began with the Title followed by (Submitted by) then I could pull the title from the recipe.
</p>
<p>
The substr() function is used to find and print the position of a string.<br />
$fourDigitPassword = substr($password,0,4);
</p>
<p>
Also see,<br />
<a href="http://www.php.net/strstr" title="">strstr()</a> and <a href="http://www.php.net/strpos" title="">strpos()</a>
</p>

<h2 id="return">Chapter Five (exercise six)</h2>
<p>Other string functions.</p>

<p>The <a href="http://www.php.net/trim" title="Trim">trim()</a> function trims spaces before and after strings but not in between.<br />
Also see, <a href="http://www.php.net/rtrim" title="Right Trim">rtrim()</a> and <a href="http://www.php.net/ltrim" title="Left Trim">ltrim()</a>.<br />
Use the <a href="http://www.php.net/ucfirst()" title="">ucfirst()</a>  function to capitalize the first letter of a string.<br />
The <a href="http://www.php.net/ucwords()" title="">ucwords()</a>   function capitalizes the start of every word in a string.<br />
The <a href="http://www.php.net/strtoupper()" title="">strtoupper()</a>   prints in all uppercase letters.<br />
Use <a href="http://www.php.net/strtolower()" title="">strtolower()</a>   to print all lowercase letters.</p>
<p>
For names, first convert the string to lowercase using the strtolower() function.<br />
Then, add the ucwords() function to capitalize each new word (in this case Name).<br />
$name = strtolower ($name);<br />
$name = ucwords ($name);
</p>
<p>
Example:<br />
$name = ucwords (strtolower($name)); // From right to left. First lower, then replace.
</p>

<p><strong>Please complete this form to submit your posting:</strong></p>

<form action="handle_post.php" method="post">
<fieldset>
<ul>
    <li>First Name: <input type="text" name="first_name" size="20" /></li>
    <li>Last Name: <input type="text" name="last_name" size="20" /></li>
    <li>Email Address: <input type="text" name="email" size="20" /></li>
    <li>Posting:<br /><textarea name="posting" rows="3" cols="30"></textarea></li>
    <li><input name="reset" type="reset" value="Reset" class="reset" /></li>
    <li><input type="submit" name="submit" value="Send My Posting" /></li>
</ul>
</fieldset>
</form>

<h2 id="six">Chapter Six</h2>
<p>Control Structures</p>

<p>The <strong>IF Conditional Statement</strong> controls how a variable is passed.<br />
Using the <a href="http://www.php.net/empty" title="">empty()</a> function can avoid errors when a variable is not defined.<br />
Curly brackets are used when a conditional statement covers more than one line.</p>

<p>The form below uses comparison operators to check if the passwords entered in the fields match by using the two variables, $password and $confirm like this:<br />
if ($password != $confirm) { // I think of the operator != as (does not equal to).<br />
print '...does not match';</p>

<p>The <strong>Switch Conditional</strong> is my favorite part of this chapter's lessons.<br />
It uses the value of the variable for each Case.</p>

<p>The <strong>For Loop</strong> is a bit tricky as I've learned in previous experiences.<br />
// The loop below shows that the value of $i starts at 1 (default is 0).<br />
// $i &lt;= 10; The value of $i is less than or equal to 10.<br />
// $i++ (mathematical operator) increases the value of $i by 1 for every loop.<br />
<span class="red">
for($i = 1; $i &lt;= 10; $i++) {<br />
    print "$i ";<br />
}</span>
</p>

<form action="handle_reg.php" method="post">
<fieldset>
<p>Please complete this form to register:</p>
<ul>
    <li>First Name: <input type="text" name="first_name" size="20" /></li>
    <li>Last Name: <input type="text" name="last_name" size="20" /></li> 
    <li>Email Address: <input type="text" name="email" size="20" /></li>
    <li>Password: <input type="password" name="password" size="20" /></li>
    <li>Confirm Password: <input type="password" name="confirm" size="20" /></li>
</ul>
<p>
Date Of Birth:
    <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>
    <select name="day">
        <option value="">Day</option>
        <?php
        
    
## Print out 31 days.
        
for($d 1$d <= 31$d++) {
            print 
"<option value=\"$d\">$d
                </option>\n"
;
        }
        
        
?>
    </select>
<input type="text" name="year" value="YYYY" size="4" />
</p>
<p>
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>
    </select>
</p>
<p><input type="submit" name="submit" value="Register" /></p>
</fieldset>
</form>

<p class="center"><a href="#top" title="Go Up!">Top of Page</a></p>

</div><!--close right-->

</div><!-- close #mainContent -->
</div><!--close #floatRight-->

Source Code for Current Page

<?php $page="four-six"?>

<!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" xml:lang="en" lang="en">
<head>
<title>Chapt. 4-6 Exercise</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/common/php.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrap">

<!-- Heading -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/h1.php'); ?>

<!-- Top Navigation Bar -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/bar-nav.php'); ?>

<!-- Related Links -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/h3.php'); ?>

<!-- Main Content -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/' $page '.php'); ?>

<!-- Bottom Navigation Bar -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/bar-nav2.php'); ?>

<!-- Foot -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/foot.php'); ?>
</div><!-- close #wrap -->

<!-- Show Source -->

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/source.php'); ?>
</body>

</html>


Head

<div id="head">
<p>Lzydaz.com</p>
<div id="heading">
<h1 id="top">PHP Study Group</h1>
<p>This page is designed as a study tool for the book<br />
<cite><a href="http://www.dmcinsights.com/phpvqs2">PHP for the World Wide Web by Larry Ullman (2nd Edition)</a></cite></p>
</div><!--close #heading-->
<img src="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/images/logo.png" height="67" width="100" alt="logo" />
</div><!-- close #head -->
<div id="head-edge">
<!--IE hates this-->
</div><!--close #head-edge-->

Navigation Bar

<div id="nav-top">
<?php $currentPage basename ($_SERVER['PHP_SELF']); ?>
<ul>
<li>
<a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/index.php" title="PHP Study Group Index"
<?php
if($currentPage=="index.php"){
    print 
'class="current"';
}
?>
    >Chapter Index</a><!--close anchor tag-->
    </li>
<li>
<a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/chapt1-3.php" title="Chapters One - Three"
<?php
if($currentPage=="chapt1-3.php"){
    print 
'class="current"';
}
?>
    >One - Three</a><!--close anchor tag-->
    </li>
    <li>
<a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/chapt4-6.php" title="Chapters Four - Six"
<?php
if($currentPage=="chapt4-6.php"){
    print 
'class="current"';
}
?>
    >Four - Six</a><!--close anchor tag-->
    </li>
    <li>
<a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/chapt7-9.php" title="Chapters Seven - Nine"
<?php
if($currentPage=="chapt7-9.php"){
    print 
'class="current"';
}
?>
    >Seven - Nine</a><!--close anchor tag-->
    </li>
    <li>
<a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/studygroup/php/chapt10-13.php" title="Chapters Ten - Thirteen"
<?php
if($currentPage=="chapt10-13.php"){
    print 
'class="current"';
}
?>
    >Ten - Thirteen</a><!--close anchor tag-->
    </li>
    </ul>
    </div> <!-- close #nav-top -->

Related Links

<div id="floatLeft">
<div id="sideHeader1">
<h3>StUdY GuiDe</h3>
<p><em>Reference links</em></p>
</div><!-- close #sideHeader1 -->

<div id="side1">
<p><a href="http://www.php.net/manual" title="php.net">PHP Functions Manual</a></p>
</div><!-- close #side1 -->

<div id="sideHeader2">
<h3>ReLaTeD</h3>
<p><em>Other links</em></p>
</div><!-- close #sideHeader2 -->

<div id="side2">
<p><a href="<?php ($_SERVER['DOCUMENT_ROOT']); ?>/phpBB3/index.php" title="Hello World">Forum</a></p>
<p><a href="http://rjoannej.com/ullmanBook/index.php" title="Joanne's Index">Joanne's Page</a></p>
<p><a href="http://krisse.tuna.fi/phpwww/index.php" title="Krisse's Index">Krisse's Page</a></p>
<p><a href="http://www.amaraland.com/studyGroup/index.php" title="KC's Index">KC's Page</a></p>

</div><!-- close #side2 -->

<div class="side3">

<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/notes/note-' $page '.php'); ?>

</div><!--close #side3-->
</div><!--close #floatLeft-->

Foot

<div id="foot-edge">
<!--IE hates this-->
</div><!--close #foot-edge-->

<div id="foot">
<?php include($_SERVER['DOCUMENT_ROOT'] . '/studygroup/php/includes/copyright.php'); ?>
<p><a href="http://validator.w3.org/check?uri=referer" rel="nofollow" title="XHTML Validator">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer" title="CSS Validator" rel="nofollow">CSS</a></p>
</div><!--close #foot-->

Copyright

<p>Bonnie Lincicome &copy; <?php echo date('Y');?> All Rights Reserved<br />
This page was last updated on <?php echo date('F d, Y H:i:s'getlastmod($filename)); ?></p>

Back to the Top