PHP Biggest of Three Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Biggest of Three Numbers?

Given three values $a, $b, and $c, the biggest is the one that is greater than or equal to both of the others.

Remember
Example: a = 14, b = 7, c = 22
  22 >= 14 and 22 >= 7  → biggest is 22

Ties: a = 10, b = 10, c = 3
  either 10 is a valid maximum
abcBiggest
1472222
1010310
-5-2-9-2
5555

How to Find the Biggest of Three Numbers in PHP

Compare pairwise (or call max) and prefer >= so ties stay correct.

  1. If $a >= $b and $a >= $c, return $a.
  2. Else if $b >= $a and $b >= $c, return $b.
  3. Otherwise return $c.
  4. Shortcut: max($a, $b, $c).
MethodIdeaTimeBest for
If-else ladderExplicit >= checksO(1)Interviews, default
max()max($a, $b, $c)O(1)Short production code
Nested maxmax($a, max($b, $c))O(1)Teaching pairwise max

Pseudocode

Pseudocode
function Biggest(a, b, c):
    if a >= b and a >= c:
        return a
    if b >= a and b >= c:
        return b
    return c

Cheat Sheet

Keep these patterns handy while coding.

TopicDetail
a wins$a >= $b && $a >= $c
b wins$b >= $a && $b >= $c
elsereturn $c
One-linermax($a, $b, $c)
Many valuesUse a running-max loop (see Maximum of Array)

Live Preview

Enter three numbers to see which is biggest.

Integers and decimals both work. Tap a chip or type your own values.

Result
a = 14, b = 7, c = 22
biggest = 22

Worked Walkthrough — 14, 7, 22

Apply the if-else ladder step by step.

Calculation
a = 14, b = 7, c = 22

Is a >= b and a >= c?
  14 >= 7  → true
  14 >= 22 → false
  → a is not the biggest

Is b >= a and b >= c?
  7 >= 14  → false
  → b is not the biggest

Otherwise return c
  biggest = 22

PHP Programs

Start with an if-else ladder, then use max(), and finish with nested pairwise max.

Example 1 — If-Else Ladder

Explicit comparisons with >= — the interview default.

php
<?php
function findBiggest($num1, $num2, $num3)
{
    if ($num1 >= $num2 && $num1 >= $num3) {
        return $num1;
    } elseif ($num2 >= $num1 && $num2 >= $num3) {
        return $num2;
    }
    return $num3;
}

$number1 = 14;
$number2 = 7;
$number3 = 22;
$result = findBiggest($number1, $number2, $number3);
echo "The biggest number is: $result\n";

How It Works

  1. If $num1 is at least as large as the other two, return it.
  2. Else if $num2 wins both comparisons, return $num2.
  3. Otherwise $num3 must be the maximum.
  4. >= keeps ties correct (equal largest values still return a valid max).
  5. Sample 14, 7, 22 prints 22.

Example 2 — max()

Short and idiomatic — PHP max() accepts any number of arguments.

php
<?php
function findBiggestWithMax($a, $b, $c)
{
    return max($a, $b, $c);
}

$a = 14;
$b = 7;
$c = 22;
echo "The biggest number is: " . findBiggestWithMax($a, $b, $c) . "\n";

How It Works

  1. max() returns the largest of its arguments.
  2. Pass all three values in one call: max($a, $b, $c).
  3. Same answer as the if-else ladder — fewer lines.
  4. Still explain the ladder in interviews before relying on the built-in.
  5. 14, 7, 22 → 22.

Example 3 — Nested max

Pairwise nesting — useful when teaching two-argument max.

php
<?php
function findBiggestNested($a, $b, $c)
{
    return max($a, max($b, $c));
}

echo findBiggestNested(5, 5, 3) . "\n";
echo findBiggestNested(-1, -4, -2) . "\n";

How It Works

  1. Inner max($b, $c) picks the larger of the last pair.
  2. Outer max($a, …) compares that result with $a.
  3. Ties: 5 vs 5 vs 3 → 5.
  4. Negatives: −1 vs −4 vs −2 → −1.
  5. Equivalent to max($a, $b, $c) for three values.

Edge Cases & Pitfalls

Check these before calling the solution done.

Ties

Prefer >= over >

Equal largest values still return a valid maximum with >=.

Negatives

Largest can still be negative

Among −5, −2, −9 the biggest is −2 — do not assume a positive answer.

N values

Do not nest forever

For many numbers, use a running-max loop or max($array).

Types

Keep types consistent

Compare numbers with numbers. Mixed strings can coerce in unexpected ways.

Time and Space Complexity

ProgramTimeExtra space
If-else / max / nestedO(1)O(1)
Running max over N valuesO(N)O(1)

Three fixed inputs need only a few comparisons — constant time and space.

Key Takeaways

  • Rule: return the value that is >= both others.
  • Ties: use >= so equal maxima still work.
  • Shortcut: max($a, $b, $c).
  • Test: 14, 7, 22 → 22; −5, −2, −9 → −2.

One line: compare three values; return the one that is not smaller than the other two.

Frequently Asked Questions

Compare with if-else using >=, or call max($a, $b, $c). Both return the largest value.
>= handles ties. If two values share the maximum, one valid answer is still returned.
Yes. PHP max() accepts any number of arguments, so max($a, $b, $c) works directly. In interviews, write the if-else version first to show the logic.
For exactly three numbers, time is O(1) and extra space is O(1).
Yes. Comparisons treat negatives the same way — among −5, −2, −9 the biggest is −2.

Next: Binary to Decimal

Convert a binary string into its decimal integer value.

Binary to decimal tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful