0
0
PhpProgramBeginner · 2 min read

PHP Program to Find Largest of Three Numbers

You can find the largest of three numbers in PHP using if statements like this: compare the first number with the second and third using if and else to print the largest number.
📋

Examples

Input5, 9, 3
OutputThe largest number is 9
Input12, 12, 7
OutputThe largest number is 12
Input-1, -5, -3
OutputThe largest number is -1
🧠

How to Think About It

To find the largest of three numbers, compare the first number with the second and third one by one using if conditions. Keep track of the biggest number found so far and finally print it.
📐

Algorithm

1
Get the three numbers as input.
2
Compare the first number with the second number.
3
Compare the larger of the first two with the third number.
4
The largest number after these comparisons is the result.
5
Print the largest number.
💻

Code

php
<?php
$a = 5;
$b = 9;
$c = 3;

if ($a >= $b && $a >= $c) {
    $largest = $a;
} elseif ($b >= $a && $b >= $c) {
    $largest = $b;
} else {
    $largest = $c;
}
echo "The largest number is $largest";
?>
Output
The largest number is 9
🔍

Dry Run

Let's trace the input values 5, 9, 3 through the code to find the largest number.

1

Initialize variables

$a = 5, $b = 9, $c = 3

2

Check if $a is largest

Is 5 >= 9 and 5 >= 3? No

3

Check if $b is largest

Is 9 >= 5 and 9 >= 3? Yes

4

Assign largest

$largest = 9

5

Print result

Output: The largest number is 9

StepConditionResult
Check $a5 >= 9 and 5 >= 3False
Check $b9 >= 5 and 9 >= 3True
Assign largest$largest = 9Done
💡

Why This Works

Step 1: Compare first number

We check if the first number is greater than or equal to both the second and third numbers using &&.

Step 2: Compare second number

If the first number is not largest, we check if the second number is greater than or equal to the other two.

Step 3: Assign largest

If neither first nor second is largest, the third number must be the largest, so we assign it.

Step 4: Print result

Finally, we print the largest number found.

🔄

Alternative Approaches

Using max() function
php
<?php
$a = 5;
$b = 9;
$c = 3;
$largest = max($a, $b, $c);
echo "The largest number is $largest";
?>
This is shorter and uses PHP's built-in function, but less educational for beginners learning comparisons.
Using nested if-else
php
<?php
$a = 5;
$b = 9;
$c = 3;
if ($a > $b) {
    if ($a > $c) {
        $largest = $a;
    } else {
        $largest = $c;
    }
} else {
    if ($b > $c) {
        $largest = $b;
    } else {
        $largest = $c;
    }
}
echo "The largest number is $largest";
?>
This uses nested conditions to find the largest, which can be clearer for some but more verbose.

Complexity: O(1) time, O(1) space

Time Complexity

The program uses a fixed number of comparisons (at most 3), so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and the largest number, so space complexity is O(1).

Which Approach is Fastest?

Using the built-in max() function is fastest and simplest, but manual comparisons help understand the logic.

ApproachTimeSpaceBest For
Manual if-elseO(1)O(1)Learning comparisons and logic
max() functionO(1)O(1)Quick and clean code
Nested if-elseO(1)O(1)Clear step-by-step logic
💡
Use PHP's built-in max() function for a quick and clean solution.
⚠️
Beginners often forget to use >= instead of >, which can cause wrong results when numbers are equal.