How to use If-else Method In PHP

PHP allows us to perform actions based on some type of conditions, or Decision Making that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. In this method, the if statement is used to execute a block of code based on a specified condition.

Example: This example illsutrates the finding the Largest Number among three Numbers using the If-Else Condition in PHP.

PHP




<?php
  
// Input the three numbers
// and store it in variable
  
$number1 = 12;
$number2 = 7;
$number3 = 15;
  
// Using the max function to find the largest number
  
if ($number1 > $number2 && $number1 > $number3) {
    echo "The largest number is: $number1\n";
} elseif ($number2 > $number1 && $number2 > $number3) {
    echo "The largest number is: $number2\n";
} else {
    echo "The largest number is: $number3\n";
}
  
?>


Output

The largest number is: 15




How to find the Largest Number among three Numbers in PHP ?

Given 3 numbers, find the largest number among the three numbers. If the first number is greater than the second and third, print it as the greatest. Otherwise, compare the second and third numbers and print the greater one.

For Instance, Consider the below example where the value of z is greater than the value of x and the value of y.

Input: x = 12, y = 7, z = 15
Output: 15

Table of Content

  • Using the max Function
  • Using Sorting Method
  • Using If-else Method

Similar Reads

Using the max( ) Function

The max() function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. Here, we will use the max() function for easily find out the largest number among the three numbers....

Using Sorting Method

...

Using If-else Method

The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. In this method, first all the three numbers are stored in the array after that it is sorted in increasing order by using sort ( ) function of PHP....

Contact Us