Find Biggest of Three Numbers in PHP
What you’ll learn
- How to find the largest of three numbers with if-else.
- Equivalent nested max approach.
- Tie handling with
>=and a live preview.
Overview
Pick the value that is not smaller than the other two. Works for negatives and ties too.
Prerequisites
Basic PHP conditionals and comparisons.
if,elseif,else.- Operators:
>,>=,&&.
What does biggest mean?
For a, b, c, biggest means value m such that m >= a, m >= b, and m >= c.
Formal note
max(a,b,c) = max(max(a,b),c).
Intuition and examples
14, 7, 22Max 22
5, 5, 3Max 5
Live preview
Algorithm
Goal: return largest among a,b,c.
1
If a >= b and a >= c, return a.
2
Else if b >= a and b >= c, return b.
3
Else return c.
📜 Pseudocode
Pseudocode
function biggest(a,b,c):
if a >= b and a >= c: return a
if b >= a and b >= c: return b
return c1
If-elseif ladder
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);
?>2
Nested max approach
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 "The biggest number is: " . findBiggest3(14, 7, 22);
?>Optimization
Three values: already O(1).
Larger N: use a loop with running maximum.
❓ FAQ
Use if-else with pairwise comparisons, or use max(max(a, b), c). Both return the largest value.
Using >= handles ties cleanly when two or three numbers are equal and still largest.
Yes. You can call max($a, $b, $c). Interviewers may still ask for manual if-else logic.
For exactly three inputs, it is O(1) time and O(1) extra space.
🔄 Input / output examples
| Inputs (a,b,c) | Output |
|---|---|
14,7,22 | The biggest number is: 22 |
5,5,3 | The biggest number is: 5 |
Edge cases and pitfalls
Ties
Equal largest values
Use >= for deterministic branches.
Negatives
All negative numbers
Largest means closest to zero.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| If-else or nested max for three numbers | O(1) | O(1) |
Summary
- Find max using comparisons.
- Handle ties with
>=. - Cost is constant for three values.
Did you know?
For three values, finding the largest needs only a few comparisons, so time and extra space are both constant.
9 people found this page helpful
