Home / Programming / PHP / 7. Understanding PHP Condition If Else, Elseif Statements

7. Understanding PHP Condition If Else, Elseif Statements

/
/
/
79 Views

The opportunity this time, let’s discuss about the conditions in PHP. Contingent explanations are utilized to perform various activities dependent on various conditions.

PHP Conditional Statements

All the time when you compose code, you need to perform various activities for various conditions. You can utilize restrictive articulations in your code to do this.

In PHP we have the accompanying contingent articulations:

  • if statement – executes some code if one condition is true
  • if…else statement – executes some code if a condition is true and another code if that condition is false
  • if…elseif…else statement – executes different codes for more than two conditions
  • switch statement – selects one of many blocks of code to be executed

The if statement executes some code if one condition is true.

See the example below. Output “Have a good day!” if the current time (HOUR) is less than 20:

The If Else Statements

The if…else statement executes some code if a condition is true and another code if that condition is false.

See the example below. Output “Have a good day!” if the current time is less than 20, and “Have a good night!” otherwise:

The If… Else If… Else Statements

The if…elseif…else statement executes different codes for more than two conditions.

Recommended  2. How to Install Apache, MySQL and PHP in Ubuntu 18.04 LTS Part 1

See the example below. Output “Have a good morning!” if the current time is less than 10, and “Have a good day!” if the current time is less than 20. Otherwise it will output “Have a good night!”:


 
  • Facebook
  • Twitter
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :