Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    the min() function

    hello,

    I have three session variables:

    PHP Code:
    $_SESSION['var1'] = "1";
    $_SESSION['var2'] = "2";
    $_SESSION['var3'] = "3"
    If I then wanted to show the lowest numerical value here was 1 I could use:

    PHP Code:
    echo min($_SESSION['var1'], $_SESSION['var2'], $_SESSION['var3']); 
    The answer would be 1. If I had the three variables again as below and wanted to know the minimum numerical value, the message echoed would be Gary. Why is this? Is there a function which finds the minimum and recognises the strings as strings and not integers? Can you test for a variable type?

    Thanks


    PHP Code:
    $_SESSION['var1'] = "1";
    $_SESSION['var2'] = "gary";
    $_SESSION['var3'] = "peter"

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    http://www.w3schools.com/php/func_math_min.asp

    oh ... I guess you have more than two numbers ....

    .

  • #3
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Not necessarily the best way of completion but:
    PHP Code:
    function mini $nums ){
    foreach (
    $nums AS $n){
    if(
    is_numeric($n)===TRUE){
    $numbers[] = $n;
    }
    }
    return 
    min($numbers);
    }

    echo 
    mini(array($_SESSION['var1'], $_SESSION['var2'], $_SESSION['var3'])); 
    Hope that helps.
    Mike

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    if I had the three variables again as below and wanted to know the minimum numerical value, the message echoed would be Gary. Why is this?
    My best guess: Strings are the equivalent of the numeric zero, and "g" comes before "p", so the lowest value there according to the function is "gary"...

    Is there a function which finds the minimum and recognises the strings as strings and not integers? Can you test for a variable type?
    you can use gettype() to find the type of a variable before you compare them with the min() function...

    Edit: Well maybe using gettype isn;t such a good idea:

    Warning Never use gettype() to test for a certain type, since the returned string may be subject to change in a future version. In addition, it is slow too, as it involves string comparison. Instead, use the is_* functions.
    then I would use is_numeric to find numeric values, like anarchy did...
    Last edited by chump2877; 02-04-2007 at 10:15 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •