Conditional Logic

If/Else

Not Right

Operators

Can use <, >, ==, !=, ||, &&, also % for modulo(ie divide 1st number by 2nd and return remainder). Note use of double equals for an equality check (remember = is used to assign variables).

Can also be used with booleans

true 1

Switch

As an alternative to if/else use switch/case.

Picked 4

Important to use break to otherwise all options below the chosen case are selected.

Ternary

A less well known operator but with many uses is the ternary operator that uses a question mark (?) and colon (:) in combination.  A ternary or three-value logic statement is constructed as follows:

(1) ? (2) : (3)

If ‘1’ is true then ‘2’ is returned.  If (1) is false then (3) is returned.

Go

This is a useful way of checking for the presence of variables and assigning default values if they are missing.

$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

The above ternary example checks to see if magic quotes are on and if not then applies the addslashes() function.