Open In App

How to Find the Smallest of given Two Nnumbers In PHP ?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers num1 and num2, the task is to find the smallest number between them. Here, we will see the different approaches to solving this problem.

Examples:

Input: 10 5
Ouput: 5

Using Conditional (Ternary) Operator

This approach uses the ternary operator to determine the smallest of two numbers in a single line of code.

Example: Illustration of Smallest of given two numbers In PHP using Conditional (Ternary) Operator.

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
$min = $num1 < $num2 ? $num1 : $num2;
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5

Using if-else Statements

This approach uses traditional if-else statements to compare two numbers and assign the smaller one to a variable.

Example: Illustration of Smallest of given two numbers In PHP Using if-else Statements.

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
if ($num1 < $num2) {
    $min = $num1;
} else {
    $min = $num2;
}
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5

Using the min Function

This approach uses the pre inbuilt min function, a built-in PHP function designed for finding the smallest value among multiple inputs.

Example: Illustration of Smallest of given two numbers In PHP Using the min Function.

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
$min = min($num1, $num2);
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5

The sort() Method

Places the numbers in an array, sorts the array in ascending order, and then retrieves the smallest element, which is now the first element in the sorted array.

Example: Illustration of Smallest of given two numbers In PHP Using Sort Method.

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
// Create an array with the numbers
$numbers = [$num1, $num2];
  
// Sort the array in ascending order
sort($numbers);
  
// The smallest number is the first element after sorting
$min = $numbers[0];
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5

Using the spaceship operator (<=>)

This approach uses the spaceship operator (<=>) for comparison, determining the smallest number through the result of the comparison.

Example: Illustration of Smallest of given two numbers In PHP Using the spaceship operator (<=>).

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
// Compare the numbers using the spaceship operator
$min = ($num1 <=> $num2) < 0 ? $num1 : $num2;
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5

Using min with an Array

Creates an array with the numbers and utilizes the min function directly with the array as an argument.

Example: Illustration of Smallest of given two numbers In PHP Using min with an Array.

PHP




<?php
  
$num1 = 10;
$num2 = 5;
  
// Use the min function with an array
$min = min([$num1, $num2]);
  
echo "The smallest number is: $min";
  
?>


Output:

The smallest number is: 5


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads