<?php
mixed max ( array $value_array )
where,
$value_array = An ARRAY containing the values
?>
<?php
mixed max ( mixed $value, mixed ...$values )
where,
$value = The comparable value
. . . . . . . . .
$values = The comparable values
?>
Comparison Operators | |||
The PHP comparison operators are used to compare two values, (NUMBER or STRING). |
|||
OPERATOR | USE | NAME | RETURNS |
== | $a == $b | EQUAL | TRUE if $a is EQUAL to $b |
!= | $a != $b | NOT EQUAL | TRUE if $a is NOT EQUAL to $b |
<> | $a <> $b | ||
=== | $a === $b | IDENTICAL | TRUE if $a is INDENTICAL to $b |
!== | $a !== $b | NOT IDENTICAL | TRUE if $a is NOT INDENTICAL to $b |
> | $a > $b | GREATER THAN | TRUE if $a is GREATER than $b |
< | $a < $b | LESS THAN | TRUE if $a is LESS than $b |
>= | $a >= $b | GREATER THAN or EQUAL | TRUE if $a is GREATER than or EQUAL to $b |
<= | $a <= $b | LESS THAN or EQUAL | TRUE if $a is LESS than or EQUAL to $b |
<=> | $a <=> $b | Spaceship | An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7. |
ed48 |
<?php
$values01a = [ 'two', 8, 18, 'thirty-two', 32, 18, 8 ];
$maxval01a = max($values01a);
$values01b = [ 2, 8, 18, 32, 32, 18, 8 ];
$maxval01b = max($values01b);
echo 'The maximum value in the ARRAY:<br>';
print_r($values01a);
echo '<br>is: ' . $maxval01a . '<br><br>';
echo 'The maximum value in the ARRAY:<br>';
print_r($values01b);
echo '<br>is: ' . $maxval01b . '<br><br>';
?>
<?php
$values02a = [ 'two', 8, 18, 'thirty-two', 32, 18, 8 ];
$values02b = [ 2, 8, 18, 32, 32, 18, 8 ];
print_r($values02a);
echo '<br><br>';
print_r($values02b);
$maxval02ab = max($values02a, $values02b);
echo '<br><br>The maximum value between the two given ARRAYS will be:';
echo '<br><br>is: ';
print_r($maxval02ab);
echo '<br><br>Merging the two given ARRAYS:<br><br>';
$mrgdval02 = array_merge($values02a, $values02b);
print_r($mrgdval02);
echo '<br><br>Then the maximum value of the new ARRAY will be: '
. max($mrgdval02) . '<br>';
?>
<?php
$val03a = 'two';
$val03b = 2;
$val03c = 8;
$val03d = 18;
$val03e = 'thirty-two';
$val03f = 32;
$max3a = max($val03a, $val03b, $val03c,
$val03d, $val03e, $val03f);
echo 'The maximum value in the given relation is: ' . $max3a . '.';
?>
<?php
$val04a = '2';
$val04b = 4;
$val04c = 8;
$val04d = 18;
$val04e = 32;
$val04f = '320';
$max4a = max($val04a, $val04b, $val04c,
$val04d, $val04e, $val04f);
echo 'The maximum value in the given relation is: ' . $max4a . '.';
?>
<?php
$val05a = 2;
$val05b = 4;
$val05c = 8;
$val05d = 18;
$val05e = 32;
$val05f = '320';
$values05 = [ 3, 5, 7, "nine", 11];
$max5a = max($val05a, $val05b, $val05c, $val05d,
$val05e, $val05f, $values05);
echo 'The maximum value in the given relation is:<br>';
print_r($max5a);
echo '.';
?>
<?php
$val06a = 2.3333;
$val06b = 8.555;
$val06c = 8;
$val06d = -18.1234;
$val06e = 32.456;
$max5a = max($val06a, $val06b, $val06c, $val06d, $val06e);
setlocale(LC_ALL, "en_US", "en-US", "american",
"american english", "american-english",
"english-american", "english-us",
"english-usa", "enu", "us", "usa");
echo 'LOCALE = en_US<br><br>The maximum value in the given relation is: ';
print_r($max5a);
echo '.';
setlocale(LC_ALL, "pt_BR.utf-8", "pt-BR.utf-8",
"portuguese-brazil", "ptb");
echo '<br><br>LOCALE = pt_BR<br><br>The maximum value in the given relation is: ';
print_r($max5a);
echo '.';
?>
<?php
try {
var_dump(max(1));
} catch (\TypeError $e) {
echo $e->getMessage() . "<br>";
}
try {
var_dump(max(array()));
} catch (\ValueError $e) {
echo $e->getMessage() . "<br>";
}
try {
var_dump(max(new stdclass));
} catch (\TypeError $e) {
echo $e->getMessage() . "<br>";
}
var_dump(max(2,1,2));
echo '<br>';
var_dump(max(2.1,2.11,2.09));
echo '<br>';
var_dump(max("", "t", "b"));
echo '<br>';
var_dump(max(false, true, false));
echo '<br>';
var_dump(max(true, false, true));
echo '<br>';
var_dump(max(1, true, false, true));
echo '<br>';
var_dump(max(0, true, false, true));
?>
<?php
echo "Testing sequences of numbers.<br><br>";
var_dump(max(2,1,2));
echo "<br>";
var_dump(max(-2,1,2));
echo "<br>";
var_dump(max(2.1,2.11,2.09));
echo "<br>";
var_dump(max("", "t", "b"));
echo "<br>";
var_dump(max(false, true, false));
echo "<br>";
var_dump(max(true, false, true));
echo "<br>";
var_dump(max(1, true, false, true));
echo "<br>";
var_dump(max(0, true, false, true));
echo "<br>";
var_dump(max(0, 1, array(2,3)));
?>
<?php
if (PHP_INT_SIZE != 8)
die("Skip this test is for 64bit platform only");
define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -MAX_64Bit - 1);
define("MIN_32Bit", -MAX_32Bit - 1);
$arrayVals = [
MAX_64Bit, MIN_64Bit,
MAX_32Bit, MIN_32Bit,
MAX_64Bit - MAX_32Bit,
MIN_64Bit - MIN_32Bit,
MAX_32Bit + 1, MIN_32Bit - 1,
MAX_32Bit * 2, (MAX_32Bit * 2) + 1,
(MAX_32Bit * 2) - 1,
MAX_64Bit -1, MIN_64Bit + 1
];
$longVals = [
MAX_64Bit, MIN_64Bit, MAX_32Bit,
MIN_32Bit, MAX_64Bit - MAX_32Bit,
MIN_64Bit - MIN_32Bit,
MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2,
(MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1,
MAX_64Bit -1, MAX_64Bit + 1, MIN_64Bit + 1,
MIN_64Bit - 1
];
var_dump(max($arrayVals));
echo '<br><br>';
var_dump(max($longVals));
?>
<?php
echo "Testing boundary conditions.<br><br>";
var_dump(max(2147483645, 2147483646) );
echo "<br>";
var_dump(max(2147483647, 2147483648) );
echo "<br>";
var_dump(max(2147483646, 2147483648) );
echo "<br>";
var_dump(max(-2147483647, -2147483646) );
echo "<br>";
var_dump(max(-2147483648, -2147483647) );
echo "<br>";
var_dump(max(-2147483649, -2147483647) );
echo "<br><br>Testing large number of arguments.<br><br>";
var_dump(max(0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,21));
?>
<?php
echo "Testing arrays.<br><br>PHP ";
echo PHP_VERSION . '<br><br>';
var_dump(max(array(2,1,2)));
echo "<br>";
var_dump(max(array(-2,1,2)));
echo "<br>";
var_dump(max(array(2.1,2.11,2.09)));
echo "<br>";
var_dump(max(array("", "t", "b")));
echo "<br>";
var_dump(max(array(false, true, false)));
echo "<br>";
var_dump(max(array(true, false, true)));
echo "<br>";
var_dump(max(array(1, true, false, true)));
echo "<br>";
var_dump(max(array(0, true, false, true)));
echo "<br>";
var_dump(max(array(0, 1, array(2,3))));
echo "<br>";
var_dump(max(array(2147483645, 2147483646)));
echo "<br>";
var_dump(max(array(2147483647, 2147483648)));
echo "<br>";
var_dump(max(array(2147483646, 2147483648)));
echo "<br>";
var_dump(max(array(-2147483647, -2147483646)));
echo "<br>";
var_dump(max(array(-2147483648, -2147483647)));
echo "<br>";
var_dump(max(array(-2147483649, -2147483647)));
?>