unknown os platform
Free Beginner PHP Tutorials - If-else Conditional statements | Whats-online.info
whats-online-info

Free Beginner PHP Tutorials - If-else Conditional statements


Free
2016:09:26 / Science and Tutorials

Learn how to use if - else conditional statement in PHP to check if a particular condition is met. Execute some code if the condition is true. See example:-





If-elseif-else Conditional statements


Conditional statements are used to make decisions based on different conditions.

In PHP we have the following conditional statements:

1. If statement - Executes some code if one condition is achieved (true)
2.If...else statement - Executes some code if a condition is achieved ( true ) and another code if that condition is not achieved (false)
3. If...else if....else statement - executes different codes for more than two conditions
4. Switch statement - selects one of many blocks of code to be executed

For this tutorial, I will discuss the first three conditional statements, and you can see how to use the switch statement.

PHP - The if Statement


If statement - Executes some code if one condition is achieved.

Syntax

if (condition) {    Execute code if condition is true; }

Example:

We will use time as our example. 

<?php $hour=date("H"); if ($hour < 20) {     echo "Have a nice day!"; } ?>

The above code will output Have a nice day if the time of the day in 24 hr clock is less than 20.

PHP - The if...else Statement


The if....else conditional statement executes some code if a condition is achieved (true) and another code if that state is not achieved (false).

Syntax

if (condition) {    Execute code if condition is true; } else {     Execute code if condition is false; }

Example:
We will use time as our example. 

<?php $hour = date("H"); if ($hour < 20) {     echo "Have a good day!"; } else {     echo "Have a good night!"; } ?>

The above code will output Have a good day! If the time of the day in 24hr clock is less than 20 (true) otherwise, output Have a good night;

PHP - The if...else if....else Statement


The if....else if...else statement executes different codes for more than two conditions.
 The else if statement executes a different statement in case the if expression evaluates to FALSE. 
The else statement executes a different statement in case the if and else if expressions evaluate to FALSE. It returns the boolean true after all the other statements have evaluated to FALSE.

Syntax

if (condition) {    Execute code if this condition is true; } elseif (condition) {     Execute code if this condition is true; } else {     Executed code if all conditions are false; }

Example
We will use time as our example. 

<?php    $hour = date("H");     if ($hour < 10) {           echo "Have a good morning!";     } elseif ($hour < 20) {          echo "Have a nice day!";    } else {        echo "Have a fantastic night!";    } ?>

The example above will output "Have a good morning!" if the current time in 24 hr clock is less than 10, and "Have a nice day!" if the current time is less than 20.  Otherwise, it will output "Have a fantastic night!":












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment