PHP | Values | Operators | Functions | Arrays | Forms | Sessions-Cookies | Classes
Functions |
Loop Functions |
Standard Functions |
User Functions
Functions are self-contained blocks of code that can be reused. A function makes a decision depending on the evaluation of a condition. A condition is declared within ( ) parenthesis. A function evaluates a condition then generates an action.
The if() function evaluates whether a condition is true or false. If the condition evaluates true, the statement is executed. If the condition evaluates false, the statement is not executed...
The condition is true. The output is...
15 is greater than 12.
This is another example. Multiple statements are declared within { } curly brackets...
The condition is true. The output is...
18 is equal to 18. I am a genius!
This is another example...
Both conditions are true. The output is...
Between 24 and 36.
This is another example...
The condition is one or the other, or both. The output is...
Either greater than 20, or less than 40, or both.
This is another example...
The condition is false. The output is...
Not greater than 20.
The else command generates alternative statements, one if the condition is true, or another if the condition is false. The { } curly brackets are not required for single statements, but are used for readability...
The condition is false. The output is...
Not greater than 6.
The elseif() function adds another condition to the if() function...
Both conditions are false. The output is...
Less than 6.
A statement block can be declared within another statement block...
The output is...
Greater than 6 but less than 12.
There is an alternative to evaluating a condition with ternary operators. The echo command holds a condition until the following statement is evaluated. The ? question mark is an operator that processes like the if() function and the : colon is an operator that processes like the else command...
The condition is false. The output is...
Not equal to 9.
This is another example...
The output is...
More than 12.
There is a third alternative to evaluating a condition. The switch() function evaluates a variable as a condition. The case command compares the variable with a value. The : operator declares that the following statement is dependent on the condition. The break command stops the evaluation if the condition is true. The default command evaluates if all statements are false...
case 7 is true. The output is...
The number is seven.