PHP Programming/The if Structure

The if statement
Conditional structures are used to control which statements get executed. They are composed of three fundamental elements:
 * if statements;
 * elseif statements; and
 * if else statements.

Conditionals in PHP are structured similarly to those found in C++ and Java. The structure begins with an if clause, which is composed of the word "if" followed by a true/false statement in parentheses <tt></tt>. The subsequent code will be contained in a block, denoted by curly braces <tt>{ }</tt>. Sometimes the braces are omitted, and only one line will follow the <tt>if</tt> statement. <tt>elseif</tt> and <tt>else</tt> clauses sometimes occur after the <tt>if</tt> clause to test for different statements.

The <tt>if</tt> clause says "If this statement is true, I want the program to execute the following statements. If it is false, then ignore these statements." In technical terms, it works like this: When an <tt>if</tt> statement is encountered, the <tt>true</tt>/<tt>false</tt> statement in parentheses is evaluated. If the statement is found to be true, the subsequent block of code contained in curly braces is executed. However, if the statement is found to be false, the program skips those lines and executes the next non-blank line.

Following the <tt>if</tt> clause are two optional clauses: <tt>else</tt> and <tt>elseif</tt>. The <tt>elseif</tt> (or <tt>else if</tt>) clause says "If the last statement was false, let's see, if this statement is true. If it is, execute the following code. If it isn't, then skip it." <tt>elseif</tt> statements are only evaluated when the preceding <tt>if</tt> statement comes out to be false. Otherwise they are skipped. Other than that, the <tt>elseif</tt> clause works just like a regular <tt>if</tt> clause. If it is true, its block is executed, if not, its block is skipped.

Finally, the <tt>else</tt> clause serves as a "catch-all" for an <tt>if</tt> statement. Essentially the <tt>else</tt> statement says "If all of the preceding tests fail, then execute this code."

Conditional expressions
Conditional values function via basic formal logic. It is important to understand how the <tt>if</tt> clause, among other clauses, evaluates these conditional values.

It is easiest to examine such with <tt>boolean</tt> values in mind, meaning that the result of a conditional value will be either TRUE or FALSE and not both. For example, if variable <tt>$x = 4</tt>, and a conditional structure is called with the expression <tt>if ($x == 4)</tt>, then the result of the expression will be TRUE, and the <tt>if</tt> structure will execute. However, if the expression is <tt>($x == 0)</tt>, then the result will be FALSE, and the code will not execute. This is simple enough.

This becomes more complicated when complex expressions are considered. The two basic operators that expressions can be conjoined with are the AND (<tt>&&</tt>) and OR (<tt>||</tt>).

Examples
We are given variables <tt>$x</tt> and <tt>$y</tt>.

Given the complex expression:

We are given a result of TRUE, because the result of both separate expressions are true. When expressions are joined with the AND operator, both sides must be true for the whole expression to be true.

Similarly:

We are given a result of <tt>TRUE</tt> as well, because at least one expression is true. When expressions are joined with the OR operator, at least one side must be true for the whole expression to be true.

Conversely,

This expression will return <tt>FALSE</tt>, because at least one expression in the whole is false.

However,

This expression will return <tt>TRUE</tt>, because at least one expression in the whole is true.

Code Blocks
A code block is one or more statements or commands that are contained between a pair of curly braces <tt>{ }</tt>. Blocks are used primarily in loops, conditionals and functions. Blocks can be nested inside one another, for instance as an <tt>if</tt> structure inside of a loop inside of a function.

If, after one of the conditional statements, there is no block of code enclosed by curly braces, only the next statement will be executed. It is recommended that you avoid using this to help prevent accidents when adding extra code after the block. The following code will not work as intended: To avoid this problem, make sure to use brackets with conditional statements, even if there is only a single line of code to be executed. This prevents the error in the above code from occurring when you add an extra line after the existing block. This code fixes the previous bug.

Shorthand notation
If you are writing a long sentence where there are some parts non-static, you may create the string using if statement. The PHP syntax allows you to do this even within one line, using following shortcut syntax:

This code uses the shorthand syntax within a line.

For more information

 * PHP Manual: Control Structures.