PHP Conditional Statements
PHP Conditional Statements
Very often when you write code, you want to perform different actions for
different conditions. You can use conditional statements in your code to do this.
Syntax
if (condition) {
code to be executed if condition is true;
}
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP - The if...elseif....else Statement
The if....elseif...else statement executes different codes for more than two
conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
PHP Loops
Often when you write code, you want the same block of code to run over and
over again in a row. Instead of adding several almost equal code-lines in a
script, we can use loops to perform a task like this.
Syntax
while (condition is true) {
code to be executed;
}
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters: