Learning PHP and MySQL
Thursday, August 03, 2006
Example 04-12 Using the DEFAULT: statement to generate an error
<?php
switch ($action) {
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
default:
echo "Error: Action must be either ADD, MODIFY, or DELETE.";
}
?>
Example 04-13 Using endswitch to end the switch definition
<?php
switch ($action):
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
default:
echo "Error: Action must be either ADD, MODIFY, or DELETE.";
endswitch;
?>
Example 04-14 A sample while loop that counts to 10
<?php
$num = 1;
while ($num <= 10){
print "Number is $num<br />\n";
$num++;
}
print 'Done.';
?>
Posted by krautgrrl on 08/03 at 08:15 AM
Chapter 4 Code • (0) Comments • (888) Trackbacks • Permalink
Chapter 4 Code • (0) Comments • (888) Trackbacks • Permalink
Example 04-15 Counting to 10 with do… while
<?php
$num = 1;
do {
echo "Number is ".$num."<br />";
$num++;
} while ($num <= 10);
echo "Done.";
?>
Example 04-16 Using break to avoid division by zero
<?php
$counter = -3;
for (; $counter < 10; $counter++){
// Check for division by zero
if ($counter == 0){
echo "Stopping to avoid division by zero.";
break;
}
echo "100/$counter<br/>";
}
?>
Example 04-17 Using continue instead of break
<?php
$counter =- 3;
for (; $counter < 10; $counter++){
// Check for division by zero
if ($counter == 0){
echo "Skipping to avoid division by zero.<br />";
continue;
}
echo "100/$counter<br />";
}
?>
Example 05-01 The ubiquitous Hello world!
<?php
echo ("Hello world!");
?>
Example 05-02 Displaying information about the PHP environment
<?
php phpinfo();
?>
Example 05-03 Creating an md5 signature
<?php
$mystring = "mystring";
$signature = md5($mystring);
echo $signature;
?>
Example 05-04 Using the string capitalization functions
<?php
// Capitalize a string
function capitalize( $str )
{
// First, convert all characters to lower case
$str = strtolower($str);
// Second, convert the first character to upper case
$str{0} = strtoupper($str{0});
echo $str;
}
capitalize("hEllo WoRld!" );
?>
Example 05-05 Creating a capitalize function with a default parameter $each
<?php
// Capitalize a string or the first letter of each word
function capitalize( $str, $each=TRUE )
{
// First, convert all characters to lower case
$str = strtolower($str);
if ($each === TRUE) {
$str = ucwords ($str);
} else {
$str = strtoupper($str);
}
echo ("$str <br>");
}
capitalize("hEllo WoRld!");
echo ("Now do the same with the each parameter set to FALSE.<br>");
capitalize("hEllo WoRld!",FALSE);
?>
Example 05-06 Modifying capitalize to take a reference parameter
<?php
function capitalize( &$str, $each=true )
{
// First, convert all characters to lower case
$str = strtolower($str);
if ($each === true) {
$str = ucwords($str);
} else {
$str{0} = strtoupper($str{0});
}
}
$str = "hEllo WoRld!";
capitalize( &$str );
echo $str;
?>
Example 05-07 A sample include file called add.php
<?php
function add( $x, $y )
{
return $x + $y;
}
?>
Posted by krautgrrl on 08/03 at 08:20 AM
Chapter 5 Code • (0) Comments • (247462) Trackbacks • Permalink
Chapter 5 Code • (0) Comments • (247462) Trackbacks • Permalink
Example 05-08 Using the include function
<?php
include('add.php');
echo add(2, 2);
?>
Example 5-9. Using include_once to include a file
<?php
include_once('add.php');
include_once('add.php');
echo add(2, 2);
?>
Friday, August 18, 2006
Example 05-09 Using include_once to include a file
<?php
include_once('add.php');
include_once('add.php');
echo add(2, 2);
?>
Statistics
This page has been viewed 279803 times
Page rendered in 0.3505 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 11
Total anonymous users: 0
Most Recent Visitor on: 09/09/2010 03:04 pm
The most visitors ever was 1103 on 11/20/2007 12:50 pm
