PHP program of decision making by using if & if-else statement
ADVERTISEMENTS
PHP program of decision making, There are you will learn how to make control decisions in the PHP program.
C program of decision making by using if & if-else statement
C++ program of decision making by using if & if-else statement
Java program of decision making by using if & if-else statement
Python program of decision making by using if & if-else statement
By using the if statement:
The syntax of the if the statement is the same C programming:
if ( test expression 1 )
{
// statement 1 to be executed if the test expression 1 is true
}
{
// statement 1 to be executed if the test expression 1 is true
}
Let us understand this example through the PHP program:
<?php
// PHP program of decision making by using if statement
$n = 5;
// true if number is less than 6
if ($n < 6) {
echo "Condition is true.\n";
}
?>
Output:
Condition is true.
By using the if...else statement:
The syntax of the if...else statement is:
if ( test expression 1 ) {
// statement 1 to be executed if the test expression 1 is true
} else {
// statement2 to be executed if the test expression 1 is false
}
// statement 1 to be executed if the test expression 1 is true
} else {
// statement2 to be executed if the test expression 1 is false
}
Example-1: In this example, you will understand how true a condition occurs.
<?php
// PHP program of decision making by using if-else statement
$n = 5;
// true if number is less than 6 otherwise it will be false
if ($n < 6) {
echo "Condition is true.\n";
} else {
echo "Condition is false.\n";
}
?>
Output:
Condition is true.
Example-2: In this example, you will understand how false a condition occurs.
<?php
// PHP program of decision making by using if-else statement
$n = 7;
// true if number is less than 6 otherwise it will be false
if ($n < 6) {
echo "Condition is true.\n";
} else {
echo "Condition is false.\n";
}
?>
Output:
Condition is false.
By using the if...else ladder:
The syntax of the if...else ladder is:
if ( test expression 1 ) {
// statement( s )
} else if( test expression 2 ) {
// statement( s )
} else if ( test expression 3 ) {
// statement( s )
}
.
.
else {
// statement( s )
}
// statement( s )
} else if( test expression 2 ) {
// statement( s )
} else if ( test expression 3 ) {
// statement( s )
}
.
.
else {
// statement( s )
}
Also, Learn:
PHP program to find the ASCII value of a character
Let us understand this example through the PHP program:
<?php
// PHP program of decision making by using the if...else if...else ladder
$n = 2;
if ($n == 0) {
echo "Fisrt condition is true.\n";
} else if ($n == 1) {
echo "Second condition is true.\n";
} else if ($n == 2) {
echo "Third condition is true.\n";
} else {
echo "Condition failed!\n";
}
?>
Output:
Third condition is true.
By using the nested if...else statement:
The syntax of the nested if...else statement is:
if ( test expression 1 ) {
else if( test expression 2 ) {
// statement(s)
} else if ( test expression 3 ) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if ( test expression m ) {
// statement(s)
}
.
.
else {
// statement(s)
}
else if( test expression 2 ) {
// statement(s)
} else if ( test expression 3 ) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if ( test expression m ) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example-1: In this example, you will understand the first input is less than the second input.
<?php
// PHP program of decision making by using the nested if-else statement
$n = 5;
$m = 7;
if ($n >= $m) {
if ($n == $m) {
echo "Result: ".$n." = ".$m."\n";
} else {
echo "Result: ".$n." > ".$m."\n";
}
} else {
echo "Result: ".$n." < ".$m."\n";
}
?>
Output:
Result: 5 < 7
Example-2: In this example, you will understand the first input is greater than the second input.
<?php
// PHP program of decision making by using the nested if-else statement
$n = 7;
$m = 5;
if ($n >= $m) {
if ($n == $m) {
echo "Result: ".$n." = ".$m."\n";
} else {
echo "Result: ".$n." > ".$m."\n";
}
} else {
echo "Result: ".$n." < ".$m."\n";
}
?>
Output:
Result: 7 > 5