PHP Basic
PHP Advanced
PHP Database
| PHP If...Else |
|
Conditional statements are used to perform different actions based on different conditions. Conditional StatementsVery often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements:
The if StatementUse the if statement to execute some code only if a specified condition is true. Syntax
The following example will output "Have a nice weekend!" if the current day is Friday:
Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true. The if...else StatementUse the if....else statement to execute some code if a condition is true and another code if a condition is false. Syntax
ExampleThe following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:
The if...elseif....else StatementUse the if....elseif...else statement to select one of several blocks of code to be executed. Syntax
ExampleThe following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!": <html> |
PHP If...Else