Conditional Statements in PHP

PHP allows us to perform actions based on different conditions. This means we can create conditions that evaluates to either true or false and based on that result we can perform certain actions. These are called Conditional Statements.

conditional statements in PHP

Conditional Statements are also known as – conditional expressions or conditional constructs.

Conditional Statements in PHP

There are few conditional statements in PHP that we can use :

  • The if statement – If condition is true, executes some code. If condition is false, ignores it.
  • The if…else statement – Executes a certain code if given condition is true and executes alternative code if condition is false.
  • The if…elseif….else statement – Combines multiple if….else statements. Performs certain actions for more than two conditions.
  • The switch…case statement – Works like if…elseif…else statements. It checks a variable against different values and executes different pieces of code corresponding to that value.

If Statements

If statement is the simplest Conditional Statement of PHP. It simply runs a block of code, if given condition returns true. If condition is not true, then it will simply ignore the code block.

Let’s check out if statement with an example :

Here, PHP will first check if the condition is true, that means it will check whether the value of $number is less than 5. If it’s true, it will echo the message we have provided. If it’s not true, nothing will be shown.

If statements will only execute the code that is placed in its block. Blocks start with { and ends with }. Code outside the block won’t be affected by the if statement.

If….Else Statements

If…else statements extends the If statements. This statement will execute a block of code if the given condition is true and execute an alternative block of code if the condition is false.

Let’s check if….else statement with example :

Here, PHP will check whether the condition is true. If true, it will execute the first block of code. If it’s not true then it will execute the second block of code, that is placed after ‘else’.

Here, our condition will return false, so the alternative code block will be executed.

Note : The ternary operator can be used instead of If…else statements as a shorthand technique.

If….elseif….else Statements

If….elseif….else statements executes different blocks of code for more than two conditions. Basically, it combines multiple If…else statements.

Let’s check out an example :

Here, PHP will go through conditions and show output if any of these returns true.

You may add as many elseif statements to an if statement as you like. If there is no else statement at the end of the control structure, PHP just continues.

Switch….case Statements

Switch statements is an alternative to If..elseif..else statements. When lots of conditions have to be checked, using if..elseif..if can be a lengthy process. Switch statements make this easier for us.

Let’s check out an example :

Here, the switch statement check a variable against different values and executes the corresponding blocks of code.

Something you should remember when working with switch statements – Once PHP finds a case statement that returns true, it will execute the action of that case as well as other case statements till the end of the switch block.

To prevent this add a break statement to the end of each case block.  The break statements stops PHP from executing the actions of all subsequent cases after its match. The default statement is used if no match is found.

I hope now you know the conditional statements in PHP and how to use them. If you face any difficulties don’t hesitate to ask through comments.

If you find this helpful, take a moment to share this with others.

Sharing is Caring
Imtiaz Rayhan
 

WordPress Enthusiast, Novice PHP Developer. Music Addict, The Beatles Fan. Conncect with me on Facebook, Google+ and Twitter.

Click Here to Leave a Comment Below 1 comments
Rohan

Awesome bro i was learning about the php language and your blog helping me so much.
Thank you for the awesome post.

Reply

Leave a Reply: