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:

You will learn all about the above four different types of decision making statements one by one divided into separate tutorials.

PHP Decision Making Example

Here is an example demonstrating how decision making statements are used in PHP program to control the program.

<html>
<head>
	<title>PHP Decision Making Example</title>
</head>
<body>
<?php 
	# this is an example on decision making statements
	# in PHP - CodesCracker
	$num1 = 5;
	$num2 = 10;
	$num3 = 15;
	if($num1>$num2)
	{
		echo "5 is greater than 10";
	}
	if($num3>$num2)
	{
		echo "15 is greater than 10";
	}
?>
</body>
</html>

Here is the sample output produced by the above decision making example in PHP:

php decision making

As you can see from the above example, if the condition provided in the if statement evaluates to be true, then only the block inside it will be executed.

Let's take another example of decision making statement in PHP:

<html>
<head>
	<title>PHP Decision Making Example</title>
</head>
<body>
<?php 
	# this is an example on decision making statements
	# in PHP - CodesCracker
	$num1 = 5;
	$num2 = 10;
	$num3 = 15;
	if($num1>$num2)
	{
		echo "5 is greater than 10";
	}
	else 
	{
		echo "5 is less than 10";
	}
	echo "<br/>";
	if($num3>$num2)
	{
		echo "15 is greater than 10";
	}
	else
	{
		echo "15 is less than 10";
	}
?>
</body>
</html>

Below is the sample output produced by the above PHP example code:

php decision making statement

As you can see from the above example, the code inside the else statement will be executed only if the condition of the if statement evaluates to be false.

Here is one more example of decision making statement in PHP:

<html>
<head>
	<title>PHP Decision Making Example</title>
</head>
<body>
<?php 
	$num1 = 5;
	$num2 = 5;
	if($num1>$num2)
	{
		echo "5 is greater than 5";
	}
	elseif($num1==$num2)
	{
		echo "5 is equal to 5";
	}
	else
	{
		echo "5 is less than 5";
	}
?>
</body>
</html>

Here is the sample output produced by the above PHP code:

php decision making

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test