Unlimited Faxes, No Fees, Dedicated Phone Number
|
|
Conditional statements in PHP 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 two conditional statements:
The If StatementIf you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. 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 when a condition is true, the lines should be enclosed within curly braces:
The Switch StatementIf you want to select one of many blocks of code to be executed, use the Switch statement. Syntax
ExampleThis is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if none of the cases are true.
|