Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 26
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Z mojej strony serwisowi należy się bardzo mocna pochwała. Nawet późna pora zgłoszenia problemu (23.00) nie przeszkodziła Darkowi w jego rozwiązaniu. Do tego poziom odpisywania na maile jest bardzo wysoki... wszystko wykłada jak cierpliwy nauczyciel. Śmiało mogę przyznać, że zamieszczone na stronach porady są rzeczowo opisane - a nie jak to bywa w innych serwisach mamy sam kod i nic poza tym! Jeszcze raz wielkie dzięki!

Damian Jarosz
Adminer.pl

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

max

(PHP 3, PHP 4, PHP 5)

max -- Znajduje największą liczbę

Opis

number max ( mixed arg1, mixed arg2, mixed argn )

max() zwraca największą liczbę spośród podanych argumentów.

Jeśli pierwszy argument jest tablicą, max() zwróci największą liczbę z tej tablicy. Jeśli pierwszy argument jest liczbą całkowitą, zmiennoprzecinkową czy łańcuchem znaków, musisz mieć conajmniej dwa argumenty, spomiędzy których funkcja max() zwróci największą wartość. Za pomocą tej funkcji możesz porównywać nieograniczoną ilość liczb.

Jeśli jedna lub więcej podanych liczb jest zmiennoprzecinkowa, wszystkie liczby zostaną potraktowane jak zmiennoprzecinkowe i także liczba zmiennoprzecinkowa zostanie zwrócona. Jeśli żadna z liczb nie będzie zmiennoprzecinkową, wszystkie zostaną potraktowane jako całkowite i zwrócona zostanie liczba całkowita.

Patrz także: min().




User Contributed Notes

tim at (NOSPAM) dot crazynot2 dot com
08-Nov-2005 10:56

In response to the previous two posters (zher0 at netcarrier dot com & walkingmantis):

I was trying to do exactly what zher0 suggested; calculate the max value of a multi-dimensional array with variably sized 'sub-arrays'.  Here is a simple little function I came up with to do just that:

<?php
function multimax( $array ) {
  
// use foreach to iterate over our input array.
  
foreach( $array as $value ) {
      
      
// check if $value is an array...
      
if( is_array($value) ) {
          
          
// ... $value is an array so recursively pass it into multimax() to
           // determine it's highest value.
          
$subvalue = multimax($value);
          
          
// if the returned $subvalue is greater than our current highest value,
           // set it as our $return value.
          
if( $subvalue > $return ) {
              
$return = $subvalue;
           }
      
       } elseif(
$value > $return) {
          
// ... $value is not an array so set the return variable if it's greater
           // than our highest value so far.
          
$return = $value;
       }
   }
  
  
// return (what should be) the highest value from any dimension.
  
return $return;
}
?>

Please note that I have only performed very limited testing on this code -- be sure to check it thoroughly if you implement it somewhere!


zher0 at netcarrier dot com
05-Jun-2005 05:31

---> walkingmantis

Your example does not work with multidimensional arrays that have 'sub arrays' of different sizes.  The following code returns 25 as the maximum value.

<?php
   $a1
= array(1,3,25);
  
$a2 = array(26,50);
  
$a3 = array(51,75);
  
$a4 = array(76, 100);
  
$a = array($a1,$a2,$a3,$a4);
      
   echo
$aMax = max(max($a));
?>

Output:
3


walkingmantis
02-Jun-2005 05:20

To find the max value of a multidimensional array, use max(max())

Use as many max()'s as there are dimensions

2D array
Input:
  <?php
   $a1
= range(1,25);
  
$a2 = range(26,50);
  
$a3 = range(51,75);
  
$a4 = range(76,100);
  
$a = array($a1,$a2,$a3,$a4);

   echo
$aMax = max(max($a));
 
?>
Output:
 100

3D array
Input:
  <?php
   $a1
= range(1,25);
  
$a2 = range(26,50);
  
$a3 = range(51,75);
  
$a4 = range(76,100);
  
$a = array($a1,$a2,$a3,$a4);
  
$aA = array($a,$a);

   echo
$aMax = max(max(max($aA)));
 
?>
Output:
 100

etc.


j dot gizmo at aon dot at
06-Mar-2004 11:45

and romik at aha dot ru,
just use typecasting.
<?
$var
=(int)$var //$var is now an int
$var=(float)$var //$var is now a float
?>


j dot gizmo at aon dot at
06-Mar-2004 11:37

regarding that "n/a" stuff, and that with the zerofilled fields, i think that:

when comparing integers with integers, the behaviour is clear.
when comparing integers and strings, the string is cast to an integer.

but when comparing strings with other strings, a string comparison is used. (strings are ordered like in a dictionary)
 eg:
"a"<"b"
"aaa"<"aab"
"aa"<"b"

this explains why "5"<"n/a" (ascii value of "5" is lower than that of "n") and why "50"<"8" (ascii of "5" vs. "8")


nonick AT 8027 DOT org
17-Dec-2003 04:50

If you are working with numbers, then you can use:

   $a = ($b > $c) ? $b : $c;

which is somewhat faster (roughly 16%) than

   $a = max($b, $c);

I tested this on several loops using integers and floats, over 1 million iterations.

I'm running PHP 4.3.1 as a module for Apache 1.3.27.


browne at bee why you dot ee dee you
16-Dec-2003 11:26

If you're looking for a way to cap a value at another, use min($value, $cap).


romik at aha dot ru
07-Sep-2003 04:05

On jesusjr at mindless dot com's note
The problem he mentioned is very important but his guess is wrong for sure.

Of course, PHP doesn't evaluate any expressions inside quotation marks. "n/a" is treated as "n/a", not as 0/0.
You can place "foo" instead of "n/a" and get same result.

My guess of such behavior is:
PHP compares 4 and "5" and treats "5" as max.
Then compares "5" which is STRING "5" to string "n/a" and the last one is going to be max.

In other words, if there is a string element whose integer value is more than that of other integer values and also string element which is really string such as "foo", "bar" etc. the last one always going to be max.

I am not sure it is a bug, so I'm leaving this comment here.

The only solution i see here is manually convert integers which has string type to integers before comparsion.
This code may help:
if ($var===strval(intval($var))) $var=intval($var);
Probably, float numbers need same conversion.

But this conversion won't help in case of "55pigs".
So, the best solution is always compare only variavbles of same type.


mikhail_kovalev at mail dot ru
14-May-2003 01:32

Note that in version 4.0.3 (the only version I tested):

max (0, 0); // returns 0.
max (0, false); // returns 0.
max (false, 0); // returns false.
max (false, false); // returns false.

As a solution use this:

(int) max (false, 0); // returns 0.
(int) max (false, false); // returns 0.


jesusjr at mindless dot com
27-Aug-2001 01:55

Be careful:

<?
     $a
= array(1,2,3,4,"5","N/A");
    
$b = array(1,2,3,4,'5','N/A');
     echo
"A: ".max($a)." : ";
     echo
"B: ".max($b)." : ";
     echo (
max($a)>0)?max($a):'max: 0';
?>

This will return:
A: N/A : B: N/A : Max: 0

I guess N/A (0/0) is equal to infinity (or >5), until it is compared to zero?


craigsrubbish at hotmail dot com
04-Aug-2000 01:18

I came across a problem that the description above doesn't seem to make clear.

In my implementation of php3 and mysql, if you have 3 database values such as (6,8,50), the max will come out as "8", as it seems to have trouble with integers that aren't the same length.

in my case this was solved by making the database field ZEROFILL'ed, so the values compared came out as (0006,0008,0050).


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt