Decision Making

Like in real life, computer programs have to make decisions. IF statement is used to make decisions in PHP.



cow_can_fly.php

PHP Code:
<?php

$cowsCanFly 
'yes'// try changing the value to 'no'

if ($cowsCanFly == 'yes') {
    echo 
'We choose to go to the moon';
}

if (
$cowsCanFly == 'no') {
    echo 
'Earth is better than heaven.';
}
== comparison operator compare value of variable $cowsCanFly with string 'yes', if matches, execute statements inside {}.



Comparison Operators in PHP

These are useful operators, you can skip to examples, you will learn to use them as required, don't have to spend much time remembering these at this point.

== Check if two values are equal.
!= Check if values are not equal
> Greater than
< Less than
>= Greater than or Equal
<= Less than or Equal


More Examples


if_greater.php

In this example, we check if student have passed exam or not.

PHP Code:
<?php

$passMark 
60;

$studentName 'Alex';
$studentMark 80;

if (
$studentMark $passMark) {
   echo 
'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
}
In this code, experiment changing value of $passMark and $studentMark.

If..Else

In previous example, we only show message if student passed exam. With help of If..Else, we can tell student when he failed exam.

if_else_greater.php

PHP Code:
<?php

$passMark 
60;

$studentName 'Alex';
$studentMark 80;

if (
$studentMark $passMark) {
   echo 
'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
} else {
   echo 
'Sorry! You failed. Good luck next time.';
}
A Bug and unlucky students...

We have a bug in our code. Try following example.

if_else_greater_bug.php


PHP Code:
<?php

$passMark 
60;

$studentName 'Unlucky Alex';
$studentMark 60;

if (
$studentMark $passMark) {
   echo 
'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
} else {
   echo 
'Sorry! You failed. Good luck next time.';
}

The student have 60 Marks, but student failed as we are checking if student have more marks than $passMark (60).

How to fix this ? Hacky way to fix this is

find

Code:
if ($studentMark > $passMark) {
Replace With

Code:
if ($studentMark > ($passMark - 1)) {
The proper way is to use Greater Than Or Equal Comparison Operator (>=)

The code will look like

if_else_greater_bug_fix.php

PHP Code:
<?php

$passMark 
60;

$studentName 'Alex';
$studentMark 60;

if (
$studentMark >= $passMark) {
   echo 
'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
} else {
   echo 
'Sorry! You failed. Good luck next time.';
}
If..Else..If

If statement can be nested, so that you can check for multiple conditions.

Lets go back and improve our cow code.

cow_version_2.php

PHP Code:
<?php

$cowsCanFly 
'what ?'// try changing the value - yes, no, what ?

if ($cowsCanFly == 'yes') {
    echo 
'We choose to go to the moon';
} else if (
$cowsCanFly == 'no') {
    echo 
'Earth is better than heaven.';
} else {
    echo 
'You should learn more about cows.';
}