Find Number Combinations in PHP
What you’ll learn
- How to list every ordering of three fixed numbers using three loops and different indices (
i,j,kall unequal). - How to list every pair from four numbers without counting the same pair twice using
j > i. - Plain-language hints on permutation vs combination, plus a live preview for three numbers.
Overview
“Combinations” in programming exercises often means: systematically try index patterns with nested loops - first all ways to fill three slots from three values, then all ways to pick two slots from four without repeating the same pair.
Three-number orderings
Six lines for three different integers - every permutation.
Pairs from four
Six unordered pairs - classic 4 choose 2 listing.
Live preview
Type three integers and see the six orderings in the page.
Prerequisites
for loops, arrays, and echo.
- Basic PHP syntax and small helper functions.
- Reading array elements with
arr[i]and comparing indices.
The idea
Think of three chairs and three people. Example 1 lists every way to seat them - first chair picks any of three array slots, second chair a different slot, third chair the last. Example 2 only picks two people out of four without caring who was “first,” so we force the first index to be smaller than the second.
No recursion yet - just loops you can trace on paper.
Live preview
Enter three integers separated by commas (same as example 1 style: 1, 2, 3). Shows all six orderings if they are pairwise different.
Algorithm
Permutations of three slots: try every (i, j, k) with i, j, k in {0,1,2} and all distinct. Pairs from n items: for each i, let j run from i+1 to n-1.
Store values
Put your numbers in an int array.
Nested loops
Use the right bounds and index rules so you do not double-count or skip valid cases.
One echo per valid combination.
📜 Pseudocode
// All orderings of three array entries (indices 0..2)
for i in 0..2:
for j in 0..2:
for k in 0..2:
if i, j, k all different:
print arr[i], arr[j], arr[k]
// All unordered pairs from indices 0..n-1
for i in 0..n-2:
for j in i+1 .. n-1:
print arr[i], arr[j]All orderings of three numbers
Array [1, 2, 3]. These are the permutations (all ways to order the three values). Mathematicians reserve “combination” for selections where order does not matter - here order does matter.
<?php
$arr = [1, 2, 3];
echo "All orderings (permutations) of the three numbers:\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
for ($k = 0; $k < 3; $k++) {
if ($i !== $j && $j !== $k && $i !== $k) {
echo $arr[$i] . " " . $arr[$j] . " " . $arr[$k] . "\n";
}
}
}
}
?>Explanation
The condition i != j && j != k && i != k ensures each printed triple uses three different positions in the array, so each value appears exactly once per line.
All pairs from four numbers
Values 10, 20, 30, 40. Each unordered pair appears once - this matches the usual “combination” idea for choosing 2 out of 4.
<?php
$arr = [10, 20, 30, 40];
$n = count($arr);
echo "All unordered pairs (choose 2 from $n):\n";
for ($i = 0; $i < $n; $i++) {
for ($j = $i + 1; $j < $n; $j++) {
echo $arr[$i] . " " . $arr[$j] . "\n";
}
}
?>Notes
Bigger sets. For permutations of many items, nested loops do not scale - people use recursion (Heap’s algorithm, etc.) or library routines.
Duplicates. If the array contains repeated values, some printed lines may look identical even when indices differ; filter or sort if your task requires unique multisets.
❓ FAQ
🔄 Input / output
Both programs use fixed arrays. To read numbers from the user, parse input with trim(fgets(STDIN)) in PHP CLI, then keep the same nested-loop logic.
Edge cases
Same value twice
Example 1 may print duplicate-looking triples; tighten logic if you need unique outputs only.
n < 2 for pairs
The inner loop never runs; no pairs to print - expected.
⏱️ Time and space complexity
| Program | Time | Extra space |
|---|---|---|
| Three nested loops, size 3 | O(1) (fixed 27 iterations) | O(1) |
Pairs from n elements | O(n^2) | O(1) |
Summary
- Orderings: distinct indices
i, j, kgive permutations of three entries. - Pairs:
j > ilists each 2-combination once. - Vocabulary: permutation vs combination depends on whether order matters.
In everyday language people say “combination lock,” but mathematically a combination ignores order while a permutation counts different orders as different. This page shows both ideas with small nested loops so you can see the pattern before heavier math.
8 people found this page helpful
