PHP and MySQL

Introduction to
PHP and MySQL
Kirkwood Center for
Continuing Education
By Fred McClurg, frmcclurg@gmail.com
© Copyright 2010, All Rights Reserved
1
Chapter Four
Decisions, Decisions
http://webcert.kirkwood.edu/~fmcclurg/co
urses/php/slides/chapter04a.conditions.ppt
2
PHP Logicals
“What is truth?”
-- Pontius Pilate
AD 30
3
Comparison Operators
Description: Comparison operators determine the
relationship between two operands and return
either a TRUE or FALSE Boolean value.
Operator
Description
==
Equal To
!=
Not Equal To
<
Less Than
>
Greater Than
<=
Less Than or Equal To
>=
Greater Than or Equal To
4
Relational Operators
Description: Relational operators determine equality between two
operands and return either a TRUE or FALSE Boolean value.
Relational Operators Example:
0; // FALSE
1; // TRUE
-1; // TRUE
$a = 0; // FALSE (assignment)
$a = 1; // TRUE (assignment)
1 == 1; // TRUE (equality)
"1" == 1; // TRUE (equality)
"1" === 1; // FALSE (identity)
'1' != 'A'; // TRUE (inequality)
1 <> 2; // TRUE (same as "!=")
5
Logical Operators
Description: Logical operators work with relational
operators to form more complex expressions.
Operator
Symbol
Description
AND
&&
True if both operands are true
OR
||
True if one or more operands are
true
XOR
n/a
True if only one operand is true
n/a
!
True if false. False if true.
6
Truth Tables (Logical AND)
Definition:
True if both operands are true.
AND TRUE FALSE
TRUE
FALSE
T
F
F
F
7
Truth Tables (Logical OR)
Definition:
True if at least one operand is true.
OR
TRUE
FALSE
TRUE FALSE
T
T
T
F
8
Truth Tables (Logical XOR)
Definition:
True if only one operand is true.
.
XOR TRUE FALSE
TRUE
FALSE
F
T
T
F
9
Truth Tables (Logical NOT)
Definition:
Negation. True if false. False if true.
!
TRUE
Result
F
FALSE
T
10
“Good Grief! You block of code.”
Description: The curly braces “{}” define the
boundaries of a code block that contain zero or
more statements.
The braces also allow you to group code so that it
corresponds with additional statements (e.g. if,
else, etc.)
Syntax:
{ // begin code block
statement;
...
} // end code block
11
The “if” Flow Chart
Description: The
“if” statement
evaluates a
condition to
determine true or
false.
When a condition
is false, the block
of code (or
statement)
skipped.
Begin
Execution
if
condition
FALSE
Continue
Execution
When a condition is
true, the block of
code (or statement)
executed.
TRUE
block of
code
12
The “if” Conditional Statement
Description: Execute a block of code if the
condition is true.
Syntax:
if ( condition )
{
statement;
}
13
The “if” Rules
Rules:
1. The condition can be any expression
(even an assignment).
2. The
curly braces “{}” are optional if
there is only one statement. However,
best practices or style guidelines may
mandate the use of braces in all
circumstances.
14
Example “if” Statements
Trace the following code:
<?php
$x = 5;
if ( $x == 5 ) // true
echo "true!<br>";
if ( $x ) // true
echo "true!<br>";
if ( TRUE ) // true
echo "true!<br>";
// is number between 2 and 7?
if ( ($x > 2) && ($x < 7) ) // true
echo "true!";
?>
15
Short Circuit Operations
In order to speed up processing, PHP (and other
languages) may “short circuit” some logical
operations. This means that the second half of a
statement may not get executed.
Example:
FALSE AND TRUE; // false
FALSE AND FALSE; // false
TRUE OR TRUE; // true
TRUE OR FALSE; // true
18
“if” Pitfalls to Avoid
<?php
if ( $x = 0 ) // assignment not equality!
echo "Never reached<br />";
if ( $x = 4 ) // assignment not equality!
echo "Always reached<br />";
// short-circuited "OR" statement
if ( TRUE || ++$x ) // not incremented
printf( "Always reached: %d<br />", $x );
// short-circuited "AND" statement
if ( FALSE && ++$x ) // not incremented
printf( "Never reached: %d<br />", $x );
printf( "\$x = %d<br />", $x );
?>
19
Bitwise operators (non short circuit-operators)
Description: Avoid using bitwise operators in an “if” statement. However, when they are used, they
function as a non short-circuited operators.
Examples:
<?php
$a = 3;
$b = 5;
if ( TRUE | ++$a ) // not short-circuit
{
printf( "Always reached<br />" );
printf( "\$a = %d<br />", $a ); // 4
}
if ( FALSE & --$b ) // not short-circuit
{
printf( "Never reached<br />" );
}
printf( "\$b = %d<br />", $b );
?>
// 4
20
The “else” Flow Chart
Description: The “else” statement handles the “not if”
condition.
When a
condition is
false, the
“else code”
(or statement)
executed.
Begin
Execution
FALSE
if
condition
TRUE
When a
condition is
true, the
“if code”
(or statement)
executed.
“if”
code
“else”
code
else branch
if branch
Continue
Execution
22
The “else” Statement Syntax
Description: Provides an alternate branch of
execution when the “if” condition is false.
Syntax:
if ( condition )
statement1;
else // if ( ! condition )
statement2;
23
An “else” Statement Example
Example:
<?php
$isTrue = FALSE;
if ( $isTrue ) // true
printf("Nothing but
the truth!");
else // false or fallback
printf("That is a lie!");
?>
24
The “else” Pitfall
Description: Beware of the dangling “else”! An “else” is always
associated with the nearest “if” regardless of paragraph
indentation.
Trace Output:
<?php
$day = 28;
if ( $day <= 29 ) // true
if ( $day == 29 ) // false
echo "Today's a leap day<br />";
else // note: this is dangling else!
echo "$day greater than 29<br />";
?>
25
Avoiding the “else” Pitfall
Description: A dangling “else” can be avoided by specifying brackets around the
“if” that is associated with the “else”.
Problem Resolved:
<?php
$day = 28;
if ( $day <= 29 ) // true
{
if ( $day == 29 ) // false
echo "Today's a leap day<br />";
}
else // dangling else fixed
echo "$day greater than 29<br />";
?>
Note: To avoid the potential dangling “else” hazard, Best Practices or Style
Guidelines may mandate the use of curly brackets regardless of the number of26
statements contained in the block of code.
Nested “if” Statements
Description: There are cases when you need to compare against a large number of alternatives. This may produce
“marching” nested if/else code.
<?php
if ( $chr = 'a' )
$rot13 = 'n';
else
if ( $chr = 'b' )
$rot13 = 'o';
else
if ( $chr = 'c' )
$rot13 = 'p';
else
if ( $chr = 'd' )
$rot13 = 'q';
else
if ( $chr = 'e' )
$rot13 = 'r';
else
if ( $chr = 'f' )
$rot13 = 's';
else
...
?>
Note: There are more elegant ways to code a ROT13 character lookup.
27
The “elseif” Syntax
Description: This statement provides a test for multiple conditions. It eliminates the need
for a nested “if-else” statement.
Syntax:
if ( condition1 )
statement1;
elseif ( condition2 )
// note: not spelled "elsif"
statement2;
elseif ( condition3 )
statement3;
...
else
statementx;
Note: The “elseif” statement may not have the same result as “else if” when using curly
28
brackets.
The “elseif” Example
Example:
<?php
$dollar = 0.50;
if ( $dollar > 100000 )
echo "Category: Upper Class";
elseif ( $dollar > 50000 )
echo "Category: Middle Class";
elseif ( $dollar > 10000 )
echo "Category: Low Class";
else // fallback
echo "Category: No Class";
?>
29
Ternary (conditional) Operator
Description: The ternary operator “?” and “:” is an
abbreviated syntax for the “if” statement
Syntax:
condition ? trueExpr : falseExpr;
Equivalent “if” Statement:
if ( condition )
trueExpression;
else
falseExpression;
30
Ternary Operator Example
<?php
$count = 4;
$name = "cact";
// make singular/plural by appending
$name .= ($count == 1) ? "us" : "i";
// make singular/plural by assignment
$verb = ($count == 1) ? "is" : "are";
$plural = ($count != 1) ? "s" : "";
printf( "There %s %d %s plant%s.",
$verb, $count, $name, $plural );
?>
31
The “switch” Statement
Description: The “switch” provides an alternate
construct for an if-elseif-else statement.
Advantage: Fall-through capability.
Disadvantage: Uses only constants. Can not use
any conditional other than “==”. Conditionals “<“,
“>”, !=, etc. are not allowed.
Pitfall: Don't forget the “break” statement!
32
The “switch” Syntax
<?php
switch ( $variable )
{
case constant1 :
statement1;
break; // optional
case constant2 :
statement2;
break; // optional
default : // optional
statementx;
}
?>
33
A “switch” Example
<?php
$year = 1960;
$month = 'June';
printf( "%s %d has ", $month, $year );
switch ( $month )
{
case 'September' :
case 'April' :
case 'June' :
case 'November' :
$len = 30;
break;
case 'February' :
$len = isLeapYear($year) ? 29 : 28;
break;
default :
$len = 31;
}
printf( "%d days", $len );
?>
34
to be continued ...
http://webcert.kirkwood.edu/~fmcclurg/c
ourses/php/slides/chapter04b.looping.ppt