Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just set up a quick benchmarking exercise:</p> <pre><code>Function time to run 1 million iterations -------------------------------------------- (int) "123": 0.55029 intval("123"): 1.0115 (183%) (int) "0": 0.42461 intval("0"): 0.95683 (225%) (int) int: 0.1502 intval(int): 0.65716 (438%) (int) array("a", "b"): 0.91264 intval(array("a", "b")): 1.47681 (162%) (int) "hello": 0.42208 intval("hello"): 0.93678 (222%) </code></pre> <p>On average, calling intval() is two and a half times slower, and the difference is the greatest if your input already is an integer.</p> <p>I'd be interested to know <em>why</em> though.</p> <hr> <p>Update: I've run the tests again, this time with coercion <code>(0 + $var)</code></p> <pre><code>| INPUT ($x) | (int) $x |intval($x) | 0 + $x | |-----------------|------------|-----------|-----------| | "123" | 0.51541 | 0.96924 | 0.33828 | | "0" | 0.42723 | 0.97418 | 0.31353 | | 123 | 0.15011 | 0.61690 | 0.15452 | | array("a", "b") | 0.8893 | 1.45109 | err! | | "hello" | 0.42618 | 0.88803 | 0.1691 | |-----------------|------------|-----------|-----------| </code></pre> <p><strong>Addendum:</strong> I've just come across a slightly unexpected behaviour which you should be aware of when choosing one of these methods:</p> <pre><code>$x = "11"; (int) $x; // int(11) intval($x); // int(11) $x + 0; // int(11) $x = "0x11"; (int) $x; // int(0) intval($x); // int(0) $x + 0; // int(17) ! $x = "011"; (int) $x; // int(11) intval($x); // int(11) $x + 0; // int(11) (not 9) </code></pre> <p><sub>Tested using PHP 5.3.1</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload