Module 0292: Conditional statements in PHP

Tak Auyeung, Ph.D.

August 31, 2017

1 About this module

2 Conditions

A condition is an expression that is true or false. The most common types of conditions are comparisons. For example, the expression 1 < 2 is true, while the expression 1 > 2 is false.

2.1 Comparisons in PHP

For a formal treatment of comparisons, please refer to the PHP manual. For math conscious, keep in mind that in the manual, when it says “TRUE if ...”, it really means “TRUE if and only if ...”

Of particular interest is the term “type juggling.” “Type juggling means the automatic conversion of types when the two sides of a comparison are not the same. A programmer must be careful about the types in a comparison because a string comparison can yield a different result from a numeric comparison.

Certain comparison operators automatically juggle types when the types of the two sides are not the same. Type juggling has a preference of numeric values. This means that if one side of a comparison is numeric and the other side is a string, the string is first interprested as a base-10 number, then the two numeric values are compared.

For a more complete set of exmaples about comparing across different types, refer to the PHP type comparison tables.

2.2 Logical operators in PHP

A logical operator combines two true/false values in a particular way, or simply work on a single true/false value. Please refer to the PHP Logical Operator page for the official descriptions.

You can see that some operators can be spelled out or use the symbolic version. The symbolic version is borrowed from C/C++, and is therefore more syntactically compatible with other programming languages.

2.3 Testing conditions

If is rather tedious to write complete PHP scripts just to test comparisons and logical operators. If you have command line interface (CLI) access to a server that has PHP installed, you can run “ad hoc” scripts quickly just to test some code.

Here is how you can do it.

Log in to the CLI of a PHP-enabled server using PuTTY (Windows) or SSH (Mac OSX or Linux).

Type the following to start php:

php

Next, type in a simple program, like this:

<?php  
  print "1" < "02";  
  print "\n";  
?>

On an empty line, type control-D on the keyboard. The script runs right away to print a value without needing to format a full HTML document.

The downside of this approach is that once is line is entered (after the ENTER key is pressed), there is no way to back up and change it. As a result, this technique is only practical for simple tests.

For more complex tests, you can use an editor to create the test file, then run the script in the CLI.

For example, use an editor to type the following into a file and call it test.php:

<?php 
  print ””1”_<_”02”_is_”.(”1” < ”02”).”n”; 
  print ”1_<_”02”_is_”.(1 < ”02”).”n”; 
?>

Then, run the script using the following command (assuming the CLI is in the folder containing the file):

php test.php

3 Conditional statements

A conditional statement specifies a branch. Based on the evaluation of a condition (an expression that evaluates to true or false), a conditional statement executes one of the branches.

Conditional statements are essential when a script needs to show something different depending on a condition. It is equally important when a script needs to perform different operations depending on factors like which button was clicked to trigger the page, the status of cookies and etc.

3.1 Simple conditional statement

The simplest conditional statement is describe here in PHP’s own manual. This kind of conditional statement only specifies what to execute if and only if a condition is true. No action takes place if and only if the condition is false.

3.2 If-else conditional statement

In general, a conditional statement specifies both what to do if and only if the condition is true as well as what to do if and only if the condition is false. This kind of conditional statements are discuss in this part of the PHP.

3.3 Multi-branch conditional statement

The most general condition statements in PHP can specify any number of branches, not just two. The trick is that conditions (each corresponding to one branch) are evaluated from top-to-bottom. As soon as one condition is evaluated true, the corresponding branch executes, and the conditional statement, as a whole, is considered completed. If and only if none of the conditions evaluate to true, then the optional else branch executes, concluding the execution of the entire conditional statement.

This form of conditional statements is discussed here in the PHP manual.

Note that the ordering of the branches matter if more than one conditions can be true at the same time. In such a case, only the branch corresponding to the first condition that evaluates to true executes. All conditions after this branch do not even get evaluated. manual.