Friday 24 February 2012

Lesson 1-15

Lesson 1

Your First Script:
The time is
<!–?php echo date(‘H:i:s’);?>
and the date is
<!–?php echo date(‘j F Y’;?>
Output:
The time is
15:33:09 and the date is
13 October 2004
The Echo Command:
<?php
echo “The time is “;
echo date(‘H:i:s’);
echo ‘ and the date is ‘;
echo date(‘j F Y’);
?>
Output:
The time is 15:59:50 and the date is 13 October 2004
Lesson 2
Understanding Variables:
<?php
$name = “Chris” ;
echo “Hello” ;
echo $name ;
?>
Output:  Hello, Chris
Expressions:
<?php
$sum = 16 + 30;
echo $sum;
?>
Output: 46
Variable in Strings:
<?php
$name = “Chris”;
echo “Hello, $name”;
echo ‘Hello, $name’;
?>
Output: Hello, Chris
            Hello, $name
Lesson 3
Conditional Statements:
<?php
$number = 5;
If ($number < 10)  {
echo “$number is less than ten”;
}
?>
Output: 5 is less than ten
Logical Operators:
<?php
$number = 8;
if ($number >= 5 and $number <= 10) {
echo “$number is between five and ten”;
}
?>
Output: 8is between five and ten
Multiple Condition Branches:
<?php
$number = 16;
if ($number < 10) {
echo “$number is less than ten”;
}
else {
echo “$number is more than ten”;
}
?>
Output: 16 is more than ten
Switch Statements:
<?php
$name = “Damon”;
Switch ($name) {
case “Damon”:
case “Shelley”:
echo “Welcome, $name, you are my friend”;
break;
case “Adolph”:
case “Saddam”:
echo “You are no friend of mine, $name”;
break;
default:
echo “I do not know who you are, $name”;
}
?>
Output: Welcome, Damon, you are my friend
While Loop:
<?php
$count = 1;
while ($count <= 10) {
$square = $count * $count;
Echo “$count squared is $square <br>;”
$count++;
}
?>
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
The do Loop:
<?php
$count = 1;
do {
$square = $count * $count;
echo “$count squared is $square <br>”;
$count++;
} while ($count <= 10);
?>
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
The for Loop:
<?php
for ($count = 1; $count <= 10; $count++) {
$square = $count * $count;
echo “$count squared is $square <br>”;
}
?>
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
Lesson 4
Using Functions:
<?php
function add_tax ($amount) {
$total = $amount * 1.09;
return $total;
}
$price = 16.00;
echo “Price before tax: $price <br>”;
echo “Price after tax: “;
echo add_tax ($price);
?>
Output:
Price before tax: 16
Price after tax: 17.44
Using Library Files:
<?php
function add_tax ($amount) {
$total = $amount * 1.09;
return $total;
}
?>
<?php
include “tax.php”;
$price = 95;
echo “Price before tax: $price <br>”;
echo “Price after tax: “;
echo add_tax ($price);
?>
Output:
Price before tax: 95
Price after tax: 103.55
Lesson 5
Arithmetic Operators:
<?php
echo 6 + 12;
?>
<br>
<?php
echo 24 – 5;
?>
<br>
<?php
echo 4 * 9;
?>
<br>
<?php
echo 48 / 12 ;
?>
<br>
<?php
echo 21 % 6;
?>
Output:
18
19
36
4
3
Incrementing and Decrementing:
<?php
for ($count = 1; $count<=10; $count++) {
echo “Count = $count<br>”;
}
?>
Output:
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Count = 6
Count = 7
Count = 8
Count = 9
Count = 10
Compound Operators:
<?php
$count = 2;
echo $count += 6;
?>
Output: 8
Numeric Data Types:
 <?php
$number = “28″;
if (is_int ($number) ) {
echo “$number is an integer”;
}
else {
echo “$number is not an integer”;
}
?>
Output: 28 is not an integer
Rounding Numbers:
<?php
echo ceil(1.3);
echo “<br>”;
echo floor(6.8);
echo “<br>”;
echo round(1.3);
echo “<br>”;
$score = 0.535;
echo round($score, 2);
echo “<br>”;
$distance = 2834;
echo round($distance, -2);
?>
Output: 4
Comparisons:
<?php
$a = 3;
$b = 7;
echo max($a, $b);
echo “<br>”;
echo min (6, 10, 23, 3, 88, 102, 5, 44);
?>
Output:
7
3
Random Numbers:
<?php
echo rand (1, 10);
?>
Output: 4
Lesson 6
Concatenation:
<?php
$phrase = “I want “;
$phrase .= “to teach “;
$phrase .= “the world “;
$phrase .= “to sing”;
echo $phrase
?>
Output: I want to teach the world to sing
Comparing Strings:
<?php
$password = “letmein”;
if ($password == “letmein”)
echo “You have a guessable password”;
echo “<br>”;
$last_name = “Thomas”;
if ($last_name < “N” )
echo “You are in group 1″;
else
echo “You are in group 2″;
?>
Output: You have a guessable password
            You are in group 2
Using printf:
<?php
$item = “The Origin of Species”;
$price = 5.99;
printf (“The price of %s is %f”, $item, $price);
?>
Output: The price of  The Origin of Species is 5.99
Format Codes:
<?php
$name1 = “Tom”;
$name2 = “Dick”;
$name3 = “Harry”;
echo “<PRE>”;
printf (“%10s \n”, $name1);
printf (“%10s \n”, $name2);
printf (“%10s \n”, $name3);
echo “</PRE>”;
?>
Output: Tom
            Dick
            Harry
Capitalization:
<?php
$phrase = “I love PHP”;
echo strtoupper ($phrase) . “<br>”;
echo strtolower ($phrase) . “<br>”;
echo ucfirst ($phrase) . “<br>”;
echo ucwords ($phrase) . “<br>”;
?>
Output:
I LOVE PHP
i love php
I love PHP
I Love PHP
Lesson 7
Outputting Contents of an Array:
<?php
$temps = array(38, 40, 49, 60, 70, 79,
84, 83, 76, 65, 54, 42);
print “<PRE>”;
print_r($temps);
print “</PRE>”;
?>
Output:
[0] => 38
[1] => 40
[2] => 49
[3] => 60
[4] => 70
[5] => 79
[6] => 84
[7] => 83
[8] => 76
[9] => 65
[10] => 54
[11] => 42
)
Looping Through an Array:
<?php
$temps = array(38, 40, 49, 60, 70, 79,
84, 83, 76, 65, 54, 42);
while (list($key, $value) = each($temps)) {
echo “Key $key has value $value <br>”;
}
?>
Output:
Key 0 has value 38
Key 1 has value 40
Key 2 has value 49
Key 3 has value 60
Key 4 has value 70
Key 5 has value 79
Key 6 has value 84
Key 7 has value 83
Key 8 has value 76
Key 9 has value 65
Key 10 has value 54
Key 11 has value 42
Associative Array:
<?php
$temps = array(“jan” => 38, “feb” => 40, “mar”=> 49,”apr” => 60, “may” => 70,
“jun” => 79,”jul” => 84, “aug” => 83,
“sep” => 76,”oct” => 65, “nov” => 54,
“dec” => 42);
print “<PRE>”;
print_r($temps);
print “</PRE>”;
?>
Output:
Array
(
[jan] => 38
[feb] => 40
[mar] => 49
[apr] => 60
[may] => 70
[jun] => 79
[jul] => 84
[aug] => 83
[sep] => 76
[oct] => 65
[nov] => 54
[dec] => 42
)
Sorting an Array:
<?php
$temps = array(“jan” => 38, “feb” => 40, “mar” => 49, “apr” => 60, “may” => 70, “jun” => 79,”jul” => 84, “aug” => 83, “sep” => 76,”oct” => 65, “nov” => 54, “dec” => 42);
asort($temps);
foreach($temps as $month => $temp) {
print “$month: $temp <br>\n”;
}
?>
Output:
jan: 38
feb: 40
dec: 42
mar: 49
nov: 54
apr: 60
oct: 65
may :70
sep: 76
jun: 79
aug: 83
jul: 84
Accessing Data From a Multi-Dimensional Array:
<?php
$temps = array (
1995 => array (“jan” => 36, “feb” => 42, “mar” => 51),
1996 => array (“jan” => 37, “feb” => 42, “mar” => 49),
1997 => array (“jan” => 34, “feb” => 40, “mar” => 50) );
echo $temps[1995]["feb"];
?>
Output: 42
Lesson 8
Using ereg:
<?php
$phrase = “I love PHP”;
if (ereg(“PHP”, $phrase)) {
echo “The expression matches”;
}
?>
Output: The expression matches
Common Character Classes:
<?php
$phrase = “I love PHP”;
if (ereg(“[[:alnum:]]”, $phrase)) {
echo “The expression matches”;
}
?>
Output: The expression matches
Testing for Position:
<?php
$phrase = “I love PHP”;
if (ereg(“^[a-z]“, $phrase)) {
echo “The expression matches”;
}
?>
Output: The expression matches
Wildcard Matching:
<?php
$word = “book”;
if (ereg(“^.oo.$”, $word)) {
echo “The expression matches”;
}
?>
Output: The expression matches
Breaking a String into Components:
<?php
$email = “chris@lightwood.net”;
if (ereg(“^([^@]+)@([a-z\-]+\.)+([a-z]{2,4})$”,
$email, $match)) {
echo “Mailbox: ” . $match[1] . “<br>”;
echo “Domain name: ” . $match[2] . “<br>”;
echo “Domain type: ” . $match[3] . “<br>”;
}
else {
echo “Email address is invalid”;
}
?>
Output:
Mailbox: chris
Domain name: lightwood
Domain type: net
Lesson 8
Searching and Replacing:
?php
$string = “(555)123-4567″;
echo ereg_replace(
“\([[:digit:]]{3}\)[[:digit:]]{3}-[[:digit:]]{4}$”,
“(XXX)XXX-XXXX”, $string);
?>
Output: (XXX)XXX-XXXX
Lesson 9
Formatting Dates:
<?php
echo date(“j F Y H:I:s”, 1000000000);
?>
Output: 8 September 2001 21:1:40
Creating Timestamps:
<?php
$time = mktime(12 + 37, 0, 0, 12, 30, 2001);
echo date(“d/m/Y H:i:s”, $time);
?>
Output: 01/01/2002 01:00:00
Getting Information About A Timestamp:
<?php
$now = getdate();
switch ($now[wday]) {
case 0:  // Sunday
case 6:  // Saturday
echo “It’s the weekend”;
break;
default: echo “It’s a weekday”;
}
?>
Output: It’s a weekday
Lesson 10
Creating and Using Objects:
<?php
class myClass {
var $myValue = “Jelly”;
function myMethod() {
echo “myValue is ” . $this->myValue . “<br>”;
}
}
$myObject = new myClass;
$myObject->myMethod();
$myObject->myValue = “Custard”;
$myObject->myMethod();
?>
Output:
myValue is Jelly
myValue is Custard
Using Third Party Classes:
 <?php
include “email_validation.php”;
$validator = new email_validation_class;
$validator->localuser = “name”;
$validator->localhost = “domain.com”;
$validator->timeout = 10;
$validator->debug = TRUE;
$email = “chris@datasnake.co.uk”;
if ($validator->ValidateEmailBox($email)) {
echo “$email is a valid email address”;
}
else {
echo “$email could not be validated”;
}
?>
Output:
Resolving host name “spam.datasnake.co.uk”….
Connecting to host address “194.1.147.25”…Failed.
chris@datasnake.co.uk is a valid email address
Lesson 11
Processing HTML Forms:
<!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=iso-8859-1″ />
<title>Untitled Document</title>
</head>
<body>
<FORM ACTION=”send_comments.php” METHOD=POST>
<TABLE>
<TR>
<TD>Your name:</TD>
<TD><INPUT TYPE=”TEXT” NAME=”name” SIZE=30></TD>
</TR>
<TR>
<TD>Your email:</TD>
<TD><INPUT TYPE=”TEXT” NAME=”email” SIZE=30></TD>
</TR>
<TR>
<TD>Your gender:</TD>
<TD><INPUT TYPE=”RADIO” NAME=”gender” VALUE=”m”> Male
<INPUT TYPE=”RADIO” NAME=”gender” VALUE=”f”> Female </TD>
</TR>
<TR>
<TD>How you found us</TD>
<TD>
<SELECT NAME=”referrer”>
<OPTION VALUE=”search”>Internet Search Engine</OPTION>
<OPTION VALUE=”tv”>TV Advertisement</OPTION>
<OPTION VALUE=”billboard”>Billboard</OPTION>
<OPTION SELECTED VALUE=”other”>Other</OPTION>
</SELECT>
</TD>
</TR>
<TR>
<TD>May we email you?</TD>
<TD><INPUT TYPE=”CHECKBOX” NAME=”may_contact”
VALUE=”Y” CHECKED></TD>
</TR>
<TR>
<TD>Comments</TD>
<TD><TEXTAREA ROWS=4 COLS=50
NAME=”comments”>Enter your comments here
</TEXTAREA></TD>
</TR>
</TABLE>
<INPUT TYPE=”SUBMIT” VALUE=”Send comments”>
</FORM>
</body>
</html>
<?php
$body = “These comments were sent via the website\n\n”;
foreach($_POST as $field => $value) {
$body .= sprintf(“%s = %s\n”, $field, $value);
}
mail(“owner@website.com”, “Comments sent via website”, $body,
‘From: “WebComments” <comments@website.com>’);
?>
<H1>Thank You</H1>
Your comments have been sent!
Output: Thank You
            Your comments have been sent!
Output: The following comments were submitted via the website
            name = Chris Newman
            email = chris@lightwood.net
            gender = m
            referrer = search
            may_contact = Y
            comments = This is my favorite website ever
Lesson 12
Defaulting the Value of a Text Input Field:
<?php
if(isset($_POST["quantity"]))
$quantity = settype($_POST["quantity"], “integer”);
else
$quantity = 1;
Output: 1 x item = $5.99
            Update quantity : 1 Change quantity
Selecting a Default Radio Button Group Item:
<?php
if (!isset($shipping))
$shipping = “economy”;
echo “Your order will be sent via $shipping shipping”;
?>
<FORM ACTION=”shipping.php” METHOD=POST>
<INPUT TYPE=”RADIO” NAME=”shipping” VALUE=”economy”
<?php if ($shipping == “economy”) echo “CHECKED”;?>> Economy
<INPUT TYPE=”RADIO” NAME=”shipping” VALUE=”standard”
<?php if ($shipping == “standard”) echo “CHECKED”;?>> Standard
<INPUT TYPE=”RADIO” NAME=”shipping” VALUE=”express”
<?php if ($shipping == “express”) echo “CHECKED”;?>> Express
<INPUT TYPE=”SUBMIT” VALUE=”Change shipping option”>
</FORM>
Output: Your order will be sent via economy shipping
  • Economy  Standard  Express  Change shipping option
Selecting a Default Item from a Menu:
?php
if (!isset($shipping))
$shipping = “economy”;
echo “Your order will be sent via $shipping shipping”;
?>
<FORM ACTION=”shipping.php” METHOD=POST>
<SELECT NAME=”shipping”>
<OPTION <?php if ($shipping == “economy”) echo “SELECTED”;?>
VALUE=”economy”>Economy</OPTION>
<OPTION <?php if ($shipping == “standard”) echo “SELECTED”;?>
VALUE=”standard”>Standard</OPTION>
<OPTION <?php if ($shipping == “express”) echo “SELECTED”;?>
VALUE=”express”>Express</OPTION>
<INPUT TYPE=”SUBMIT” VALUE=”Change shipping option”>
</FORM>
Output: Your order will be sent via economy shipping
              Economy Change shipping option
Creating a Multiple-Option Selection Using Check Boxes:
<?php
function generate_checkboxes($name,
$options, $default=array()) {
if (!is_array($default))
$default = array();
foreach($options as $value => $label) {
$html .= “<INPUT TYPE=CHECKBOX “;
if (in_array($value, $default))
$html .= “CHECKED “;
$html .= “NAME=\”{$name}[]\” VALUE=\”$value\”>”;
$html .= $label . “<br>”;
}
return($html);
}
$options = array(“movies” => “Going to the movies”,
“music”  => “Listening to music”,
“sport”  => “Playing or watching sports”,
“travel” => “Traveling”);
$html = generate_checkboxes(“interests”,
$options, $interests);
?>
<H1>Please select your interests</H1>
<FORM ACTION=”interests.php” METHOD=POST>
<?php print $html;?>