Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php find greater of two numbers

Tags:

php

I am a straight novice in PHP. So, please do not down vote my question as it may seem very silly. I have a code to check if a number is greater than another number in php.

if($num1 > $num2)
{
    echo $num1
}
else
{
    echo $num2
}

The above code is the exact one in my file. But no output is being shown. Any help will be appreciated.

like image 275
John Smith Avatar asked Sep 23 '13 13:09

John Smith


People also ask

How to find greater number in PHP?

The max() function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. The max() function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.

How do I find the greatest value in an array in PHP?

The max() function returns the highest value in an array, or the highest value of several specified values.

How do you find the minimum and maximum value in PHP?

$min = min($numbers); $max = max($numbers); Option 2. (Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use array_map : $numbers = array_map(function($details) { return $details['Weight']; }, $array);


2 Answers

The easiest way to do that is:

echo max($num1, $num2);
like image 81
SteeveDroz Avatar answered Oct 16 '22 08:10

SteeveDroz


John, the answer is very simple. You have not added a ; at the end of your statement. Change your code to the following :-

if($num1 > $num2)
{
    echo $num1;
}
else
{
    echo $num2;
}

This code will surely work.

like image 22
h2O Avatar answered Oct 16 '22 08:10

h2O