You are on page 1of 30

Orange

BELT
… OnlyKiosk Coding …
Make Choices
OnlyKiosk.com
Make Decisions
OnlyKiosk.com
IF Statement
IF

Your PHP Program Can Now Make Decisions Itself

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

} What If I Need To Deal With More Than One Condition?

We need logical operator to connect the two


conditions.

OnlyKiosk.com
Logical
($number > 5 Operator
$number < 10)

Logical Expression

TRUE OR FALSE IF Statement

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

1. make decisions based on the value of $variable;


2. make decisions based on whether $variable is
REGARDED as TRUE or FALSE;

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”; }

lots and lots of elseif ( … ) { … }


OnlyKiosk.com
In Case Your Condition Is a Variable

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

$value = expression ? value 1 : value 2;

TRUE if(condition True){


$value = value 1;
FALSE } else{
$value = value 2;
}

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?

5< $number < 10?

$number > 5 TRUE


simultaneously

$number < 10 TRUE

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

condition_1 Logical_Operator conditional_2

TRUE or Operation Results: TRUE or


FALSE if( TRUE or FALSE ){ FALSE


}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

You might also like