Find Number Combinations in PHP

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Nested loops

What you’ll learn

  • How to list every ordering of three fixed numbers using three loops and different indices (i, j, k all 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.

Duplicate inputs can repeat the same triple; the PHP program can be extended to skip duplicates if needed.

Live result
Press “List orderings”.

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.

Print

One echo per valid combination.

📜 Pseudocode

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]
1

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
<?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.

2

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
<?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

We show two classic patterns: (1) every way to order three given numbers - that is a permutation; (2) every way to pick two different numbers from a list when order does not matter - pairs with i < j, which matches the math idea of a 2-combination.
You need one loop index for each position in the triple. Requiring i, j, k all different makes sure you use each array slot once per line, so you list each ordering of the three values.
There are 3! = 6 permutations. If two numbers are equal, some lines can repeat - you can add checks to skip duplicates if needed.
That way each unordered pair appears once. Without it you would get both (a,b) and (b,a), which is the same pair for combinations.
Example 2 is the small case of choosing 2 from 4. The general nCr formula counts how many such subsets exist; the loops visit each subset explicitly.
Example 1 does O(1) iterations for fixed size 3 (six checks). Example 2 does O(n^2) nested iterations for n elements in the pair case; three nested loops for triples from n would be O(n^3) if you scanned all i,j,k.

🔄 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

Duplicates

Same value twice

Example 1 may print duplicate-looking triples; tighten logic if you need unique outputs only.

Small n

n < 2 for pairs

The inner loop never runs; no pairs to print - expected.

⏱️ Time and space complexity

ProgramTimeExtra space
Three nested loops, size 3O(1) (fixed 27 iterations)O(1)
Pairs from n elementsO(n^2)O(1)

Summary

  • Orderings: distinct indices i, j, k give permutations of three entries.
  • Pairs: j > i lists each 2-combination once.
  • Vocabulary: permutation vs combination depends on whether order matters.
Did you know?

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.

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.

8 people found this page helpful