Professional Documents
Culture Documents
380 Orange-2-If
380 Orange-2-If
BELT
… OnlyKiosk Coding …
Make Choices
OnlyKiosk.com
Make Decisions
OnlyKiosk.com
IF Statement
IF
OnlyKiosk.com
The Most Simple One:
if( condition ){
PHP Statements_1;
IF
} else{
PHP Statements_2;
}
else{ } can be neglected
OnlyKiosk.com
How It Works
PHP Statemtents_1;
TRUE
PHP Evaluates (condition) Evaluation Result
Not TRUE
PHP Statemtents_2;
OnlyKiosk.com
TRUE or FALSE
Comparison Expression:
$number> 9 or $number == 10;
condition
Variable:
$variable
OnlyKiosk.com
Project-1
I want a program that can help
pick numbers that are smaller
than 10.
OnlyKiosk.com
A More Complicated One:
if( condition ){
PHP Statements_1;
IF
} elseif( condition_2 ){
The Purple Part Can
PHP Statements_3; Be Repeated
} else{
PHP Statements_2;
}
OnlyKiosk.com
Project-2
I want a program that can help
pick numbers that are smaller
than 10 but also bigger than 5.
OnlyKiosk.com
Using if inside if:
if( condition 1 ){
if(condition 2 ){
PHP Statements_1;
IF
}
}else{
PHP Statements_2;
}
OnlyKiosk.com
if($number >10 ? $number<5){
PHP Statements;
IF
OnlyKiosk.com
Logical
($number > 5 Operator
$number < 10)
Logical Expression
OnlyKiosk.com
Logical Operator List
(http://www.w3schools.com/php/php_operators.asp)
$number<10
$number>5 and / &&
OnlyKiosk.com
Comparison Expression
Like: $number > 5
( condition )
Variable
OnlyKiosk.com
( condition )
$Variable
OnlyKiosk.com
if ( $fruit == ‘apple’ ){
echo “ the price is…”;
}elseif( $fruit == ‘peach’ ){
echo “ the price is…”;
}elseif( $fruit == ‘banana’ ){
echo “ the price is…”;
…
}else{ echo “not in stock”; }
switch( $fruit ){
SWITCH
case “apple” :
PHP Statements ; can be repeated
break;
default:
PHP Statements ; optional
}
OnlyKiosk.com
In Case You Have Lots Of Conditions
switch($value){
SWITCH
case value_1: if($condition_1){
PHP Statements; …
break; }
…
case value_2: elseif($conditioin_2){
PHP Statements; …
break; }
…
default: else{
PHP Statements; …
} }
OnlyKiosk.com
In Case You See This:
SWITCH
switch($variable){
case value_1: // fall through
case value_2:
PHP Statements_1;
break;
When $variable == value_1 or value_2,
…
PHP execute PHP Statements_1
default:
PHP Statements;
}
OnlyKiosk.com
SWITCH
switch($condition):
case value_1:
PHP Statements;
break; replace { and }
…
endswitch;
OnlyKiosk.com
Write A Price Management Program For A Fruit Store
OnlyKiosk.com
In Case Your Condition Is a Variable
switch( $fruit ){
SWITCH
case “apple”:
echo ” the price is …”;
break;
case “peach”:
echo ” the price is …”;
break;
case “banana”:
echo ” the price is …”;
break;
…
default:
echo “ not in stock”;
}
OnlyKiosk.com
Alternative Way:
Ternary Operator
OnlyKiosk.com
TRUE or FALSE
Comparison Expression:
$number> 9 or $number == 10;
condition
Variable:
$variable
make decisions based on the value of $variable
FALSE:
1. any integer or float that is zero;
2. ‘0’ or “0”;
3. Null;
OnlyKiosk.com
What If I Want My Program To Tell Me If A Number Is Between 5 And 10?
OnlyKiosk.com
Logic Operator
– how to deal with more than one condition?
$number > 5
&& $number < 10
TRUE TRUE
simultaneously
OnlyKiosk.com
Anatomy Of Logical Operation
…
}else{
…
}
OnlyKiosk.com
if( $variable== ‘value_1’ ){
… The switch statement is used to
perform different actions based on
}elseif( $variable==‘value_2’ ){ different conditions.
…
}elseif( $variable==‘value_3’ ){
…
}else{
…
}
OnlyKiosk.com
A Little Complicated One:
if(condition){
PHP Statements;
IF
} else{
PHP Statements;
}
OnlyKiosk.com