codescracker


php

PHP Decision Making



« Previous Tutorial Next Tutorial »


PHP decision making statements are used to perform different types of actions based on different actions. There are following types of decision making statements available in PHP:

PHP if Statement

PHP if statement is used to execute a block of code if the specified condition is true. Here is the general form to use if statement in PHP:

if(condition)
{
    code to be executed here, if the condition evaluates to true;
}

Example

Here is an example uses if statement in PHP. This example will output "Have a Nice Day from CodesCracker.com" if the current time (HOUR) is less than 20:

<!DOCTYPE html>
<html>
<body>

<?php
	$t = date("H");

	if($t < "20")
	{
		echo "Have a Nice Day from codescracker.com";
	}	
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP if Statement

PHP if-else Statement

PHP if-else statement is similar to the PHP if statement. But in PHP if-else statement, there is an optional else statement available to be executed if the specified condition evaluates to false.

In other word, PHP if-else statement is used executed a block of code (present in the if block) only if the specified condition evaluates to true, otherwise another block of code (present in the else block) will be executed. Here is the general form to use if-else statement in PHP:

if(condition)
{
    code to be executed here, if the condition is true;
}
else
{
    code to be executed here, if the condition is false;
}

Example

Here is an example, uses if-else statement in PHP. This example will output "Have a Nice Day from CodesCracker.com" if the current time is less than 20, and "Have a Nice Night from CodesCracker.com" otherwise:

<!DOCTYPE html>
<html>
<body>

<?php
$t = date("H");

if($t < "20")
{
     echo "Have a Nice Day from codescracker.com";
}
else
{
     echo "Have a Nice Night from codescracker.com";
}
?>

</body>
</html>

Below is the output of this PHP script:

PHP if...else Statement

PHP if-elseif-else Statement

PHP if-elseif-else statement is used to perform some action based on more than one condition. You can use PHP if-elseif-else statement to specify a new condition to be tested, if the first condition evaluates to be false. Here is the general form of PHP if-elseif-else statement:

if(condition1)
{
    code to be executed here, if condition1 is true;
}
elseif(condition1)
{
    code to be executed here, if condition2 is true;
}
else
{
    code to be executed here, if both the condition is false;
}

Example

Here is an example uses PHP if-elseif-else statement. This example example will output "Have a Nice Morning from CodesCracker.com" if the current time is less than 10, and "Have a Nice Day from CodesCracker.com" if the current time is less than 20. Otherwise it will output "Have a Nice Night from CodesCracker.com"

<!DOCTYPE html>
<html>
<body>

<?php
$t = date("H");
if($t < "10")
{
     echo "Have a Nice Morning from codescracker.com";
}
elseif($t < "20")
{
     echo "Have a Nice Day from codescracker.com";
}
else
{
     echo "Have a Nice Night from codescracker.com";
}
?>

</body>
</html>

It will display the following result

PHP if...else Statement

PHP Switch Statement

PHP switch statement is basically used to select one of many blocks of code to be executed. You will learn about PHP switch statement in the next tutorial.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test