If-statements are used to only execute a code block when certain conditions are true. You will use logical operators and comparison operators to code these conditions.
If you do not have a programming environment handy, you can run this lessons code using: PHP Fiddle
Code Block: It is basically a group of statements that work togeher to perform a task. In the examples below, you will see each code block is inside { }.
Before we begin the lesson, take a momment to think about the concept. Can you think of some situations when you might want to use an if statement?
There are plenty! It is very common to want to execute different code depending on the circumstance. Example: When you play a game, the code executed will be different if you pass the level than if you fail the level. There are an infinate number of cases where if statements could be used.
By the way, this is where programming starts getting fun. Once you learn if-statements you can start making applications that actually do stuff.
Commander Coral is a picky eater who loves web development. Her friend, Sondra, invited her out to dinner. To RSVP, Coral is making a web application using HTML and PHP. Sondra will be sent a link to the app. She will select a type of food: Southern, Korean, Greek, Vegatarian. Coral's response to the dinner invite will depend on Sondra's food choice. Coral is writting PHP logic to make sure Sondra gets the right message.
// Declare dinner variable
$dinner = "";
// The printed message depends on the value of $dinner
if ($dinner == "Southern") {
echo "Sorry, I keep Kosher. Maybe some other time.";
}
elseif ($dinner == "Vegatarian") {
echo "Sounds great! I will be there at 7";
}
elseif ($dinner == "Korean") {
echo "Send me the menu and I will get back to you.";
}
else {
echo "Greek? Did you say Greek? I am on the way!";
}
➼ $dinner = "";
The variable $dinner is declared and initialized as an empty string. It's value will be determined
by the choice Sondra makes on the HTML form.
➼ if ($dinner == "Southern")
then Coral will not go.
Output: Sorry, I keep Kosher. Maybe some other time.
➼ elseif ($dinner == "Vegatarian")
then Coral will attend.
Output: Sounds great! I will be there at 7
➼ elseif ($dinner == "Korean")
then Coral wants to see the menu before deciding.
Output: Send me the menu and I will get back to you.
➼ else
Since Greek is the only other option, Coral is assuming Greek. The else
code block will execute in all cases not specified.
Output: Greek? Did you say Greek? I am on the way!
// Define variable: use a random number generator
$randInt = rand(1,11);
// The printed message will depend on the random number
if ($randInt < 5){
echo "less than 5... ";
}
elseif ($randInt > 5) {
echo "greater than 5.. ";
}
elseif ($randInt==5) {
echo "5... ";
}
else {
echo "11... ";
}
➼ $randInt = rand(1,11);
$randInt is a variable equal to a randomly generated integer. rand() is a PHP function.
It randomly generates an integer within a specific range. In our example a random number will be generated between
1 and 11.
➼ if ($randInt < 5)
If the random number is less than 5 the message is "less than 5... "
➼ elseif ($randInt > 5)
If the random number is greater than 5,the message is "greater than 5.. "
➼ elseif ($randInt==5)
If the random number is equal to 5, the message is "5... "
➼ else
If none of the specified conditions are met, the message is "11... "
Try running the appove code: PHP Fiddle
The comparrison operator '!=' means 'not equal'.
If $a != $b
, then $a and $b do not contain the same value.
// Not Equal
if ($randInt != $randInt2) {
echo "Not Equal";
}
// Else
else {
echo "Equal";
}
➼ if ($randInt != $randInt2)
If the value of $randInt is not equal to the value of $randInt2
Output: Not Equal
➼ else
if the condition
($randInt != $randInt2) is not true, than $randInt must be equal to $randInt2.
Output: Yay! It's Tuesday!!!
The comparrison operator '<=' means 'less than or equal to'.
If $a <= $b
, then $a is either less than $b or it is equal to $b
// Less than or equal
if ($randInt <= $randInt2){
echo "less than or equal to... ";
}
// Else
else {
echo "greater than... ";
}
➼ if ($randInt <= $randInt2)
If the value of $randInt is less than or it is equal to the value of $randInt2
Output: less than or equal to...
➼ else
if the previous
statement is not true, then $randInt must be greater than $randInt2.
Output: greater than...
The comparrison operator '>=' means 'greater than or equal to'.
If $a >= $b
, then $a is either less than $b or it is equal to $b
// Greater than or equal
if ($randInt >= $randInt2){
echo "greater than or equal to... ";
}
// Else
else {
echo "less than... ";
}
➼ if ($randInt >= $randInt2)
If the value of $randInt is greater than or it is equal to the value of $randInt2
Output: greater than or equal to...
➼ else
if the previous
statement is not true, then $randInt must be less than $randInt2.
Output: greater than...
The logical operator 'and' means the same as '&&'.
You can use either one. Just make sure you are consistant in your application. Pick one or the other.
Don't mix it up. It is important to always keep your code clear, consistant and easy to read.
If ($a == $b and $a == $c)
, then $a is equal to both $b and $c
// Define Variables
$x = 6;
$y = 0;
// if statement
if ($x > 5 and $y == 0) {
echo "It worked";
}
➼ $x = 6;
The variables $x and $y are defined
$y = 0;
➼ if ($x > 5 and $y == 0)
Output: It worked
The logical operator 'or' means the same as '||'.
You can use either one. Just make sure you are consistant.
If ($a == $b or $a == $c)
, then $a is either equal to $b, equal to $c,
or equal to both $b and $c
// Define Variables
$x = 6;
$y = 0;
// if statement
if ($x > 5 or $y == 0) {
echo "It worked";
}
➼ $x = 6;
The variables $x and $y are defined
$y = 0;
➼ if ($x > 5 or $y == 0)
Output: It worked
The logical operator 'xor' means that either the first or second
statement are true. However, both statements can not be true.
If ($a == $b xor $a == $c)
, then $a is either equal to $b or $c. However,
$a cannot be equal to $b and $c
// Define Variables
$x = 6;
$y = 0;
// if statement
if ($x > 5 xor $y == 0) {
echo "It worked";
}
➼ $x = 6;
The variables $x and $y are defined
$y = 0;
➼
if ($x > 5 xor $y == 0)
There would be no output for this code, beacuse both statements are true.