Conditionals

In its simpliest form, a conditional executes a block of code if a certain condition exists. If it doesn't exist, the code will be ignored. A variation on that principal is a conditional that executes a block of code if a certain condition exists, and executes a different block of code if the certain condition does not exist.

IF Statement

In plain english, an If statement reads: "If this condition is true, execute this code". The syntax looks like so:

if (condition) { code to be performed }

Here are some examples:

<?php if (10 > 5) { echo "The laws of mathematics have not changed!<br />\n"; } if (date('Y') >= 2018) { echo "We found Bin Laden.<br />\n"; } $favWebTutorials = "Sandersongs.com"; if ($favWebTutorials) { echo $favWebTutorials." rocks!<br />\n"; } if (false) { echo "This will never display"; } ?>

which displays thus:

The laws of mathematics have not changed!
Sandersongs.com rocks!

IF/ELSE Statement

In plain english, an If/else statement reads: "If this condition is true, execute this code, else execute this other code". The syntax looks like so:

if (condition) { code to be performed } else { alternate code to be performed }

Here are some examples:

<?php if (10 - 10) { echo "The laws of mathematics have changed!<br />\n"; } else { echo "What's going on here?<br />\n"; } if (1) { echo "The Yankees win again.<br />\n"; } else { "Baseball is fun to watch!<br />\n"; } if (date('j') < 15) { echo "This month just started!<br />\n"; } else { echo "This month is almost over"; } ?>

which displays thus:

What's going on here?
The Yankees win again.
This month is almost over

IF/ELSEIF/ELSE Statement

In plain english, an If/elseif/else statement reads: "If this condition is true, execute this code, else if this other condition is true, execute this other code, else execute this default code". The syntax looks like so:

if (condition) { code to be performed }
else if (condition) { alternate code to be performed }
else { default code to be performed }

Here is an example:

<?php if (date('G') < 12) { echo "Good morning!<br />\n"; } else if (date('G') < 18) { echo "Good afternoon!<br />\n"; } else { echo "Good evening!<br />\n"; } ?>

which displays thus (remember, this server is on MST):

Good afternoon!

SWITCH Statement

The Switch statement is an if/elseif/else statement in another form. The Switch statement takes a variable and runs different code depending on the value of the variable.

switch ($variable) {
case "one thing" : // code to do something
  break;
case "something else" : // code to do something else
  break;
case "yet another thing" : // code to do still something else
  break;
default : // default code
}

This is the functional equivalent of:

if ($variable == "one thing") { // code to do something }
else if ($variable == "something else") { // code to do something else }
else if ($variable == "yet another thing") { // code to do still something else }
else { // default code }

The break command tells the computer to stop where it is, forget the rest of the code within the Switch statement and continue after the end of the Switch statement. break ends execution of the current for, foreach, while, do..while or switch structure. More about this when we learn loops.

Here is an example:

<?php switch (date('D')) { case "Mon" : echo "Back to work day.<br />\n"; break; case "Tue" : echo "Four more days to go.<br />\n"; break; case "Wed" : echo "Over-the-hump day.<br />\n"; break; case "Thu" : echo "The end is in sight.<br />\n"; break; case "Fri" : echo "TGIF.<br />\n"; break; default : echo "The weekend is here!<br />\n"; } ?>

which displays thus:

The weekend is here!

Other Conditional Considerations

For the hard-core programmer, there is a short-cut to the if/else statement called the Ternary Operator. The two statements following are equal:

if (condition) {this;} else {that;} and
(condition) ? this : that;

PHP also has an alternate syntax for conditional statements. When your files are hundreds of lines long and you have complex, nested conditional statements, you sometimes lose your place in the hierarchy of curly braces. In a nutshell, you replace the curly braces with a colon ( : ) and an end statement. Consider the following:

if (condition) : //do some code;
elseif (other condition) : //do some other code;
else : //do default code;
endif;


and

switch (condition) :
case "one thing" : // code to do something
  break;
case "something else" : // code to do something else
  break;
case "yet another thing" : // code to do still something else
  break;
default : // default code
endswitch;

Here are the comparison operators you may use in any conditional:

< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
== is equal to
=== is equal to and variable data types are also equal
!= or <> is not equal to
&& (and) both conditions must be true
|| (or) either condition must be true
! condition must be not true (false)

Arrays   < <  PREVIOUS   Table of Contents NEXT  > >   Loops

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2017, by Bill Sanders, all rights reserved.
This domain had 4,007 different visits in the last 30 days.
435,484 hits on this domain since 24 Nov 2006.
http://sandersongs.com/PHPsqlCourse/PHP05.php
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 12:32 pm, MST
This file took 0.0084 seconds to process.