A conditional evaluates a Boolean expression, that is, something that returns a value. If the expression is true, it will execute everything inside its block of code. A block of code is a group of statements enclosed by {}.

Conditions are expressions that PHP tests or evaluates to see whether they are true or false. Conditions are used in complex statements to determine whether a block of simple statements should be executed. To set up conditions, you compare values using comparison operators.

Examples of Conditions

  • Are two values equal?
  • Is one value larger or smaller than another?
  • Does a string match a pattern? 
  • Does the ZIP code have five numeric characters?

You can also set up conditions in which you ask two or more questions.

Comparing Values

You can compare numbers or strings to see whether they are equal, whether one is larger than the other, or whether they are not equal. You compare values with comparison operators. PHP evaluates the comparison and returns true or false.

You can compare both numbers and strings. Strings are compared alphabetically, with all uppercase characters coming before any lowercase characters. For example, SS comes before Sa.

Strings are compared based on their ASCII code. In the ASCII character set, each character is assigned an ASCII code that corresponds to a number between 0 and 127. When strings are compared, they are compared based on this code. For example, the number that represents the comma is 44. The period corresponds to 46. Therefore, if a period and a comma are compared, the period is evaluated as larger.

Checking Variable Content

Sometimes you just need to know whether a variable exists or what type of data is in the variable.

isset($varname): True if variable is set, even if nothing is stored in it.

empty($varname): True if value is 0 or is a string with no characters in it or is not set.

You can also test what type of data is in the variable.

  • is_int($number)
  • is_array($var2)
  • is_float($number)
  • is_null($var1)
  • is_numeric($string)
  • is_string($string)

You can test for a negative condition, as well, by using an exclamation point (!) in front of the expression.

Joining Multiple Comparisons

Often you need to ask more than one question to determine your condition. This requires you to join comparisons, which have the following the general format:

comparison1 and|or|xor comparison2 and|or|xor comparison3

Comparisons are connected by one of the following three words:

  1. and: Both comparisons are true.
  2. or: One or both comparisons are true.
  3. xor: Only one of the comparisons is true.

Conditional Statements

A conditional statement executes a block of statements only when certain conditions are true. Two useful types of conditional statements are:

  1. An if statement: Sets up a condition and tests it. If the condition is true, a block of statements is executed.

  2. A switch statement: Sets up a list of alternative conditions. It tests for the true condition and executes the appropriate block of statements.

1. Using if Statements

A conditional is defined by the keyword if followed by a Boolean expression in parentheses and by a block of code. If the expression is true, it will execute the block, otherwise it will skip it. You can increase the power of conditionals by adding the keyword else. This tells PHP to execute some block of code if the previous conditions were not satisfied.

Finally, you can also add an elseif keyword followed by another condition and a block of code to continue asking PHP for more conditions. You can add as many elseif as you need after an if. If you add an else, it has to be the last one of the chain of conditions. Also keep in mind that as soon as PHP finds a condition that resolves to true, it will stop evaluating the rest of conditions.

if (condition)
{
block of statements
}
elseif (condition)
{
block of statements
}
else
{
block of statements
}

2. Using switch Statements

Another control structure similar to if…else is switch…case. This structure evaluates only one expression, and executes the block depending on its value. For most situations, the if conditional statement works best. However, sometimes you have a list of conditions and want to execute different statements for each condition.

switch ($variable_name)
{
case value:
block of statements;
break;
case value:
block of statements;
break;
...
default:
block of statements;
break;
}

The switch statement tests the value of $variable_name. The script then skips to the case section for that value and executes statements until it reaches a break statement or the end of the switch statement. If there is no case section for the value of $variable_name, the script executes the default section.

When the case matches the current value of the expression, PHP executes the code inside it. As soon as PHP finds a break statement, it exits the switch…case. In case none of the cases are suitable for the expression, PHP executes the default, if there is one, but that is optional.

The breaks are mandatory if you want to exit the switch…case. If you do not specify any, PHP will keep on executing statements, even if it encounters a new case.