Conditional Statements in PHP – If/Else/Switch


Conditional statements are used to test conditions in PHP. You test a certain condition and then perform an action based on the condition. Like when a condition is true do this and when the condition is false, do this instead. These statements make use of the logical as well as comparison operators for forming conditions.

If/Else If/Else

Using this statement, you can check a condition and perform action on whether the condition is true or false. You can further check additional conditions if the first condition turned out to be false.

Example:

You can combine multiple conditions using logical operators.

Example:

SWITCH Statement

This statement is also used to perform different actions based on conditions. When the number of conditions are huge or come serially, use this statement.

Example:

Note: The break; statement is used to escape the condition. You can use it with other conditional or looping statements as well and it will break the execution of the following lines of code.

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers