Learning PHP and MySQL

Thursday, August 03, 2006

Example 04-03 Lefthand expressions

<?php
3  
=  $locations;  //  bad  -  a  value  can  not  be  assign  to  the  literal  3
$a  +  $b  =  $c;  //bad  -  the  expression  on  the  left  isn't  one  variable
$c  =  $a  +  $b;  //OK
$stores  =  "Becker"."  "."Furniture";  //  OK
?>

Posted by krautgrrl on 08/03 at 08:08 AM
Chapter 4 Code • (0) Comments • (29) TrackbacksPermalink

Example 04-04 Order of precedence

2  +  4  -  5    ==  1;
4  -  5  +  2  ==  1;

4  *  5  /  2  ==  10;
5  /  2  *  4  ==  10;

2  +  4  -  5    ==  1;
4  -  5  +  2  ==  1;

4  *  5  /  2  ==  10;
5  /  2  *  4  ==  10;

Posted by krautgrrl on 08/03 at 08:09 AM
Chapter 4 Code • (0) Comments • (74) TrackbacksPermalink

Example 04-05 The multiplication is done last because of the override

<?php
echo  2  *  3  +  4  +  1;
echo  
2  *  (3  +  4  +  1);
?>

Posted by krautgrrl on 08/03 at 08:10 AM
Chapter 4 Code • (0) Comments • (412) TrackbacksPermalink

Example 04-06 Else and if statements

<?php
if  ($username  ==  "Admin"){
  
echo  ('Welcome  to  the  admin  page.');
}
else  {
  
echo  ('Welcome  to  the  user  page.');
}
?>

Posted by krautgrrl on 08/03 at 08:10 AM
Chapter 4 Code • (0) Comments • (102) TrackbacksPermalink

Example 04-07 Checking multiple conditions

<?php
if  ($username  ==  "Admin"){
  
echo  ('Welcome  to  the  admin  page.');
}
elseif  ($username  ==  "Guest"){
  
echo  ('Please  take  a  look  around.');
}
else  {
  
echo  ("Welcome  back,  $username.");
}
?>

Posted by krautgrrl on 08/03 at 08:11 AM
Chapter 4 Code • (0) Comments • (495) TrackbacksPermalink

Example 04-08 Using the ? operator to create a message

<?php
$logged_in  
=  TRUE;
$user  =  "Admin";
$banner  =  ($logged_in==TRUE)?"Welcome  back  $user!":"Please  login.";
echo  
"$banner";
?>

Posted by krautgrrl on 08/03 at 08:12 AM
Chapter 4 Code • (1) Comments • (352) TrackbacksPermalink

Example 04-09 Using if to test for multiple values

<?php
if  ($action  ==  "ADD")  {
  
echo  "Perform  actions  for  adding.";
  echo  
"As  many  statements  as  you  like  can  be  in  each  block.";
}
elseif  ($action  ==  "MODIFY")  {
  
echo  "Perform  actions  for  modifying.";
}
elseif  ($action  ==  "DELETE")  {
  
echo  "Perform  actions  for  deleting.";
}
?>

Posted by krautgrrl on 08/03 at 08:12 AM
Chapter 4 Code • (0) Comments • (1) TrackbacksPermalink

Example 04-10 Using switch to test for multiple values

<?php
switch  ($action)  {
case  "ADD":
  echo  
"Perform  actions  for  adding.";
  echo  
"As  many  statements  as  you  like  can  be  in  each  block.";
  break;
case  
"MODIFY":
  echo  
"Perform  actions  for  modifying.";
  break;
case  
"DELETE":
  echo  
"Perform  actions  for  deleting.";
  break;
}
?>

Posted by krautgrrl on 08/03 at 08:13 AM
Chapter 4 Code • (0) Comments • (286) TrackbacksPermalink

Example 04-11 What happens when there are no break keywords

<?php
$action
=”ASSEMBLE  ORDER”;
switch  (
$action)  {
case  "ASSEMBLE  ORDER":
  echo  
"Perform  actions  for  order  assembly.<br>";
case  
"PACKAGE":
  echo  
"Perform  actions  for  packing.<br>";
case  
"SHIP":
  echo  
"Perform  actions  for  shipping.<br>";
?>

Posted by krautgrrl on 08/03 at 08:13 AM
Chapter 4 Code • (0) Comments • (382) TrackbacksPermalink

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.";
}
?>

Posted by krautgrrl on 08/03 at 08:14 AM
Chapter 4 Code • (0) Comments • (1) TrackbacksPermalink

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;
?>

Posted by krautgrrl on 08/03 at 08:14 AM
Chapter 4 Code • (0) Comments • (1) TrackbacksPermalink

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) TrackbacksPermalink

Example 04-15 Counting to 10 with do… while

<?php
$num  
=  1;

do  
{
  
echo  "Number    is  ".$num."<br  />";
  
$num++;
}  while  ($num  <=  10);

echo  
"Done.";
?>

Posted by krautgrrl on 08/03 at 08:15 AM
Chapter 4 Code • (0) Comments • (2) TrackbacksPermalink

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/>";
}
?>

Posted by krautgrrl on 08/03 at 08:16 AM
Chapter 4 Code • (0) Comments • (1) TrackbacksPermalink

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  />";
}
?>

Posted by krautgrrl on 08/03 at 08:16 AM
Chapter 4 Code • (0) Comments • (1) TrackbacksPermalink
Page 3 of 15 pages « First  <  1 2 3 4 5 >  Last »

Statistics

This page has been viewed 168257 times
Page rendered in 0.4506 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: 19
Total anonymous users: 0
Most Recent Visitor on: 08/28/2008 10:53 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine