The If code block allows you to selectively execute program code based on when a condition is met. For instance, you may want to output a message based on the score for a student in the class. An example of this is shown below…
// one-line if statement if ($score >= 60) echo 'You Pass!'; // if statement in a code block if ($score >= 90) { echo 'You Pass!' $grade = 'A'; }
Using the Else keyword, you can execute a second set of code to execute when the condition is not met. This allows you to perform two separate actions based on a single condition. An example of this is shown below…
// if-else statement if ($score >= 60) { echo 'You Pass!'; } else { echo 'Sorry, You Failed'; } // if-else as a single line statement if ($score >= 60) echo 'You Pass!' else 'Sorry, You Failed';
Finally, the ElseIf keyword allows you to group multiple conditions into a If expression. Using the scores example shown previously, we could display the grade for a student using the following conditional:
// if-else statement if ($score >= 90) { echo 'Grade: A'; } elseif ($score >= 80) { echo 'Grade: B'; } elseif ($score >= 70) { echo 'Grade: C'; } elseif ($score >= 60) { echo 'Grade: D'; } else { echo 'Sorry, You Failed'; }
You should take special notice of the evaluation order for your conditionals. They are always done from top to bottom. Once a single condition is met, only the code for that condition will be executed, all other conditions will be skipped.
As an alternative to writing a series of elseif statements, you can use a switch statement to match a set of distinct values.
switch ($grade) { case 'A' : echo 'Excellent!'; break; case 'B' : echo 'Great!'; break; case 'C' : echo 'Good Job'; break; case 'D' : echo 'Good Effort'; break; default: echo 'Thanks for Trying'; }
After the switch keyword, you put an expression in parentheses that you want to use in your comparisons. Next a set of curly braces define a block of literal values which are tested to match the expression shown. The first value which matches will be executed.
The break statement exits the entire switch statement. Without it, the processor would continue trying to match other values even after the first match is found. The special value default will match any other value besides the ones which are explicitly defined.
Use the for statement to iterate over a list of items or to repeat a block of statements a specified number of times. The for statement has the following syntax (which is borrowed from the C language):
for (initializer; comparison; incrementer) { // php statements to repeat }
The initializer is where you initialize your variable. Basically, this represents your starting place or starting condition. It usually assigns a variable a value (e.g.: $i = 1). The comparison statement is the condition under which your for loop should continue repeating. As long as this expressions evaluates to a true statement, the loop will repeat. The incrementer is an action to perform (usually incrementing a counter) after each repetition of the loop.
Now let's look at a very simple example of a for loop which counts from one to five:
// count from one to five! for ($i = 1; $i <= 5; $i++) { echo 'count = ' . $i . '<br>'; }
As you can see, the variable $i is initialized to one ($i = 1) in the initializer. The condition ($i ⇐ 10) can be read as: repeat the loop as long as $i ⇐ 5. Finally, the incrementer advances the counter by one after each iteration of the loop. The output of this code would be:
count = 1 count = 2 count = 3 count = 4 count = 5