Find Biggest of Three Numbers in PHP

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Conditionals

What You’ll Learn

Finding the biggest of three numbers is a classic conditional warm-up. This tutorial covers the comparison rule, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.

Goal

Largest of a, b, c

Return the value that is not smaller than the other two.

If-Elseif Ladder

Explicit logic

Compare with >= so ties still pick a valid maximum.

Built-in max

max($a, $b, $c)

Short and clear for real code; still explain the ladder in interviews.

Ties & Negatives

Edge cases

Equal largest values and all-negative inputs still work the same way.

Live Preview

Try a, b, c

Enter three numbers and see the maximum instantly in the browser.

O(1)

Complexity

A fixed number of comparisons — constant time and constant extra space.

Introduction

Given three numbers a, b, and c, the task is to find the biggest value — the one that is greater than or equal to both of the others.

You can write an explicit if-elseif-else ladder, nest max calls, or use PHP’s built-in max($a, $b, $c). If two or three values tie for largest, returning any one of those tied values is valid.

Why it matters?

It trains clear branching, tie handling, and the habit of explaining O(1) complexity for fixed-size inputs.

Key Highlights

Prefer >=

Handles equal largest values without special cases.

Negatives OK

Among negatives, the largest is the least negative.

Two Styles

Write the ladder for interviews; use max in apps.

Constant Cost

Three fixed inputs need only a few comparisons.

In short: compare a, b, and c; return the value that is greater than or equal to the other two.

📝 Problem & Approach

Given three numbers a, b, and c, return the maximum among them.

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

Inputs & Outputs

ItemTypeDescription
a, b, cint / floatThree numbers to compare (ints or floats both work).
Return / printsame typeThe largest value among the three (any tied max is fine).

Minimal workflow

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

Method comparison

MethodIdeaNotes
If-elseif ladderExplicit comparisons with >=Best for showing interview logic
Built-in maxmax($a, $b, $c)Shortest production style

⚡ Quick Reference

GoalPattern
a is biggest$a >= $b && $a >= $c
b is biggest$b >= $a && $b >= $c
Otherwisereturn c
One-linermax($a, $b, $c)
Nested maxmax($a, max($b, $c))
Many valuesmax($values) or a running-max loop

📋 If-Elseif vs max() vs Nested max

Same answer — different clarity and interview signaling.

If-elseif
compare >=

Shows branching clearly; preferred whiteboard style

max($a, $b, $c)
built-in

Idiomatic PHP for real applications

Nested max2
max2(max2(a,b),c)

Same idea; useful when teaching pairwise max

Interview tip
ladder first

Write if-elseif, then mention max as a shortcut

Context

When This Problem Shows Up

Reach for biggest-of-three drills when branching and comparisons matter.

  1. Interview warm-ups

    Quick check of if-elseif structure and tie handling.

  2. Teaching conditionals

    First multi-branch program after simple if/else.

  3. Gateway to N-max

    Natural step toward a running-max loop over a list.

  4. Small fixed comparisons

    UI scores, three sensors, or three candidate prices.

  5. Not for huge lists alone

    For many values, use a loop or max($array) instead of nested ifs.

Key benefit: one tiny problem that covers branching, ties, negatives, and O(1) complexity talk.

🔮 Live Preview

Enter three numbers and check the biggest value instantly.

Integers and decimals both work.

Live result
Press "Run" to see the biggest value.

Examples Gallery

Three complete PHP programs — if-elseif ladder, built-in max, and nested max. Click View Output to reveal sample console results.

📚 Getting Started

Explicit comparisons — the interview default.

Example 1 — If-Elseif-Else Approach

Check a, then b, otherwise return c — using >= for ties.

php
<?php
function findBiggest(int $a, int $b, int $c): int
{
    if ($a >= $b && $a >= $c) return $a;
    if ($b >= $a && $b >= $c) return $b;
    return $c;
}

$a = 14; $b = 7; $c = 22;
echo "The biggest number is: " . findBiggest($a, $b, $c);
?>

How It Works

The first branch wins when $a is a maximum. The second branch covers when $b is a maximum. Otherwise $c must be biggest.

⚡ Idiomatic PHP

Same answer with the built-in helper.

Example 2 — Built-in max()

Pass all three arguments directly to max.

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

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

How It Works

max compares its arguments and returns the largest. Great for production code after you have already explained the comparison logic.

🔁 Pairwise Style

Build the three-way max from two-way max calls.

Example 3 — Nested max2

First take the larger of a and b, then compare with c.

php
<?php
function max2(int $x, int $y): int
{
    return ($x > $y) ? $x : $y;
}

function findBiggest3(int $a, int $b, int $c): int
{
    return max2(max2($a, $b), $c);
}

echo findBiggest3(5, 5, 3) . PHP_EOL;
echo findBiggest3(-1, -4, -2) . PHP_EOL;
?>

How It Works

max2($a, $b) reduces two values to one candidate; then max2(…, $c) finishes the job. The tie case 5,5,3 returns 5; the all-negative case returns -1.

🧠 How the Algorithm Decides

1

Test a

If a >= b and a >= c, a is a maximum — return it.

Branch
2

Test b

Otherwise, if b >= a and b >= c, return b.

Branch
3

Else c

If the first two checks fail, c must be the biggest.

Fallback
=

Maximum found

Return that value — ties are already handled by >=.

🔎 Worked Walkthrough — 14, 7, 22

Trace the if-elseif ladder with a = 14, b = 7, c = 22.

CheckConditionResult
a biggest?14 >= 7 and 14 >= 22False (14 < 22)
b biggest?7 >= 14 and 7 >= 22False
Fallbackreturn c22

Tie example: for 5, 5, 3, the first branch 5 >= 5 and 5 >= 3 is true, so the answer is 5.

Use Cases

Where biggest-of-three checks show up beyond the interview prompt.

1. Interview Warm-Ups

Tests if-elseif structure and clear return values.

Example: write findBiggest(a, b, c).

2. Teaching Branches

Makes multi-way decisions feel concrete.

Example: chalkboard walkthrough of 14, 7, 22.

3. Small Scoreboards

Pick the best of three fixed candidates.

Example: three quiz attempts.

4. Gateway to N-Max

Leads into running-max loops over lists.

Example: max of an array.

5. Complexity Practice

Argue O(1) time for a fixed three inputs.

Example: “how many comparisons?”

6. Tie Talk

Shows why >= is safer than strict >.

Example: inputs 5, 5, 3.

Pro Tip: keep a pure findBiggest helper and echo outside it — easier to test ties and negatives.

Advantages

Why this pattern works well in interviews and classwork.

  1. 1. Tiny and Clear

    A few comparisons map directly to the definition of maximum.

  2. 2. Constant Cost

    Fixed three inputs mean O(1) time and O(1) extra space.

  3. 3. Easy Built-in Shortcut

    max($a, $b, $c) keeps application code short after you know the logic.

  4. 4. Natural Edge-Case Story

    Ties and negatives give interviewers clear follow-up questions.

Pro Tip: say “return any tied maximum” before coding — it justifies >= immediately.

Usage Tips

Small habits that keep biggest-of-three code interview-ready.

  1. 1. Use >= for Ties

    Strict > can skip equal largest values depending on branch order.

  2. 2. Lead with the Ladder

    Write if-elseif first in interviews, then mention max.

  3. 3. Spot-Check Negatives

    Assert max(-1, -4, -2) == -1 before calling it done.

  4. 4. Keep the Helper Pure

    Return the value; echo outside the function.

  5. 5. Scale Carefully

    For many numbers, switch to a loop or max($array).

Pro Tip: dry-run both a clear winner (14, 7, 22) and a tie (5, 5, 3) on paper once.

Common Pitfalls

Mistakes that commonly break biggest-of-three solutions.

  1. 1. Using Strict > Carelessly

    Tie cases may fall through branches unexpectedly depending on order.

    → Prefer >= when any tied maximum is acceptable.

  2. 2. Assuming Negatives Are Invalid

    Maximum among negatives is still well-defined.

    → Test an all-negative triple.

  3. 3. Comparing Strings by Accident

    Input read as text compares lexicographically, not numerically.

    → Convert with int / float before comparing.

  4. 4. Only Calling Built-in max in Interviews

    Interviewers often want to see the comparison logic.

    → Write the ladder, then mention max as a shortcut.

  5. 5. Exploding Ifs for N Values

    Nested conditions do not scale past three inputs.

    → Use a running maximum when N grows.

Edge Cases

Check these inputs before calling the solution done.

Ties

Equal largest values

For 5, 5, 3, the answer is still 5.

All equal

Any value works

For 2, 2, 2, return 2.

Negatives

All values negative

The greatest value is the least negative number.

Zeros

Mix with negatives

0, -1, -5 → 0.

Floats

Same idea

Comparisons work for floats; widen parameter types (e.g. float) if needed.

Input type

Parse before compare

Do not compare string digits as text.

⚖️ Facts Worth Knowing

Handy follow-ups interviewers sometimes ask.

  • Associative. max($a, max($b, $c)) equals max(max($a, $b), $c).
  • Idempotent. max(x, x) = x, which is why ties are easy.
  • Dual problem. Biggest of three mirrors smallest of three with min / <=.
  • Fixed size. Complexity stays O(1) only while the input count is fixed at three.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Verify classics

  • 14, 7, 22 → 22
  • 5, 5, 3 → 5
  • -1, -4, -2 → -1

2. Write smallest of three

  • Mirror the ladder with <=
  • Or use min(a, b, c)

3. Extend to a list

  • Running max over N values
  • State O(N) time

4. Implement both styles

  • If-elseif and max
  • Assert they agree on a test set

Notes

  • Definition. Biggest means greater than or equal to each of the other two.
  • Use >= so equal largest values still return correctly.
  • In interviews, show the ladder first; mention max($a, $b, $c) second.
  • State O(1) time and O(1) extra space for exactly three inputs.

Quick Takeaway: compare a, b, and c with a few >= checks (or max) and return the largest.

⏱️ Time and Space Complexity

ProgramTimeExtra space
If-elseif checks for 3 numbersO(1)O(1)
Built-in max($a, $b, $c)O(1)O(1)
Nested max($a, max($b, $c))O(1)O(1)
Wrap Up

🎉 Conclusion

Finding the biggest of three numbers is a clean branching exercise: compare with >=, handle ties, and return the winner. Master the if-elseif ladder first, then use max($a, $b, $c) when brevity matters.

Practice the three examples above, then continue to binary-to-decimal for a classic base-conversion warm-up.

Prefer >= for ties, test negatives, and state O(1) time for exactly three inputs.

💡 Best Practices

✅ Do

  • Compare with >= for clear ties
  • Show the if-elseif ladder in interviews
  • Test ties and all-negative inputs
  • Mention max($a, $b, $c) as a shortcut
  • State O(1) time and space

❌ Don’t

  • Rely only on max when logic must be shown
  • Compare unparsed strings as numbers
  • Assume negatives are invalid
  • Nest ifs endlessly for long lists
  • Forget that any tied maximum is valid

Key Takeaways

Knowledge Unlocked

Five things to remember about biggest of three

Pick the maximum the interview-friendly way.

5
Core concepts
if 02

Ladder

if / elseif / else

Code
M 03

max()

Built-in shortcut

Code
= 04

Ties

Any max is OK

Edge
O 05

Complexity

O(1) time

Analysis

❓ Frequently Asked Questions

Use if-elseif with pairwise comparisons, or use max($a, $b, $c). Both return the largest value.
Using >= handles ties clearly. If two numbers are equal and largest, one valid maximum is still returned correctly.
Yes. You can call max($a, $b, $c). Interviewers may still ask for manual if-else logic.
For exactly three numbers, time complexity is O(1) and extra space is O(1).
Yes. Comparisons work the same way for negative values too.
Use a loop with a running maximum, or max() on an array of values.
Any of them is a valid answer — they all share the same maximum value.
Yes. It is equivalent and still O(1) for three fixed arguments.

Did you Know? 🔊

For three values, finding the largest needs only a few comparisons, so time and extra space are both constant.

Continue to Binary to Decimal

Learn how to convert a binary number into its decimal equivalent in PHP.

Binary to decimal tutorial →

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.

9 people found this page helpful