php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3289 min() and max() have wierd problems when using a mixed int and string array.
Submitted: 2000-01-23 19:09 UTC Modified: 2002-09-30 17:26 UTC
From: c dot just at its dot uq dot edu dot au Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.14 OS: Linux and Win32
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: c dot just at its dot uq dot edu dot au
New email:
PHP Version: OS:

 

 [2000-01-23 19:09 UTC] c dot just at its dot uq dot edu dot au
I have found a very strange problem with the min() and max() functions. 
I just thought I'd check with the list first before I submitted it.

It seems both functions have problems when there are a mix of strings and integers. 
Even thought the strings are actually numbers(no alpha chars)
Max has a problem when there is a string first.
I have tested this in 
PHP Linux 3.0.14
PHP Win32 3.0.13
PHP Win32 3.0.5



Try the code below out.

<?
echo "Array values<BR>";
$stats = array(40,10,120,100,380);
for($i=0;$i<count($stats);$i++)
	echo "$i - ".$stats[$i]."<BR>";
	
echo "<HR>";

$stats = array(40,"10",120,100,"380");
$high = max($stats);
$low = min($stats);
echo "Array with Mixed String and Integers<BR>";
echo "Min - ".$low."<br>";
echo "Max - ".$high."<br><HR>";

$stats = array("40","10",120,100,"380");
$high = max($stats);
$low = min($stats);
echo "Array with Mixed String and Integers, With String as first element<BR>";
echo "Min - ".$low."<br>";
echo "Max - ".$high."<br><HR>";

$stats = array(40,10,120,100,380);
$high = max($stats);
$low = min($stats);
echo "Array with Integers Only<BR>";
echo "Min - ".$low."<br>";
echo "Max - ".$high."<br><HR>";

$stats = array(40,"10",120,100,"380");
// CONVERT ALL VALUES TO INTEGERS
for ($i=0;$i<count($stats);$i++)
  $stats[$i]=intval($stats[$i]);

$high = max($stats);
$low = min($stats);
echo "Array with Mixed String and Integers converted to integers<BR>";
echo "Min - ".$low."<br>";
echo "Max - ".$high."<br><HR>";

?> 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-30 17:26 UTC] hholzgra@php.net
fixed in php4
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 11:01:37 2025 UTC