The PHP Functions min() and max()

Today a small snippet, which I frequently use in my WordPress Widgets. It is about the determination of minimum and maximum values. Here is a piece of code from the WordPress Widget Recent Comments:

if ( !$number = (int) $instance['number'] )
    $number = 10;
else if ( $number < 1 )
    $number = 1;
else if ( $number > 15 )
    $number = 15;

With the usage of the mathematical functions min() and max() we can do it in one line:

$number = isset ( $instance['number'] ) ? min ( max ( 1, ( int ) $instance['number'] ), 15 ) : 10;

Posted

in

by

Comments

8 responses to “The PHP Functions min() and max()”

  1. Andrew Nacin Avatar

    Sure, it works, but don’t you find that less legible?

  2. Michael Avatar

    @Andrew Most of the time I have to check several values. That makes the code so long. And I also think the function is faster.

  3. Andrew Nacin Avatar

    I consider that a challenge, so I ran a quick speed test. 🙂

    The if statement is 2-3 times faster than min/max. The language construct is just much faster here because of how the conditionals are handled. Going to out perform functions every time.

    Also, your code doesn’t function the same for the value of 0 (or any non-numeric value). You end up with 1, instead of 10.

  4. Michael Avatar

    Ok, you won. :p And yes, my code does not exactly the same as the widget code.

  5. Frank Avatar

    @Andrew: where do you have run your speedtest: 5.4 or smaller? It is give many different values in different php-version, but i think: importent is, the developer can read his code and it works.

    Thanks for reply and this is great, that we can discuss about our posts!
    Maybe you will write a post for the WP Adventcalender on WP Engineer?

  6. Joseph Scott Avatar

    I agree with Nacin, reading the first version is much easier.

    I also confirmed the speed difference. I tested this on php-5.3.3 on Ubuntu using this script – http://pastebin.com/mEwU9EVX – (basic, but gets the job done).

    After several runs the single line version gave 3.0994415283203E-5 as the fastest time. The other runs were very close to this time as well.

    For the if/else version 2.0027160644531E-5 was the fastest and the other runs were very close to that time.

    Memory is also an issue, the single line version used 184 bytes more than the if/else version. Based on numbers provided by memory_get_peak_usage(). Not big, but a difference none the less.

    I stripped out the benchmark lines from that script and looked at the number of ops for each version using VLD:

    if/else: 22
    single line: 25

    In general fewer ops means less work, means better performance. (this very broad, assumes the cost of each operation is roughly the same, which may or may not be the case).

    For this particular case everything points in the direction of using the if/else style.

  7. Frank Avatar

    Hi Joseph,
    thanks for share your tests and insights – i think the discussion on a post is often the best insight and clarification.
    Also thanks for read us!

  8. Thomas Scholz Avatar

    Single line version with readable, simple comparison:

    $number > 10 and $number = 10 or $number < 0 and $number = 0;