note 91404 modified in function.is-int by danbrown
| From: | danbrown@php.net | Date: | Tue, 09 Jun 2009 20:22:36 +0000 |
| Subject: | note 91404 modified in function.is-int by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156653@lists.php.net to get a copy of this message | ||
If you're concerned with speed, you can save yourself about 50% of the time it takes to check
whether or not something is an integer pretty easily...
Running through 10,000 iterations of each of the following 3 methods outputs fairly consistently...
<?php
$foo = is_int($i);
// 0.0032429695129395 seconds
$foo = ((int) $i) == $i;
// 0.0020270347595215 seconds
$foo = ((int) $i) === $i;
// 0.001600980758667 seconds
?>
So if you want the best performance and know you're not going to be passed a string that needs
to be typed as an integer, use === to compare the type as well.
--was--
If you're concerned with speed, you can save yourself about 50% of the time it takes to check
whether or not something is an integer pretty easily...
Running through 10,000 iterations of each of the following 3 methods outputs fairly consistently...
<?
$foo = is_int($i);
// 0.0032429695129395 seconds
$foo = ((int) $i) == $i;
// 0.0020270347595215 seconds
$foo = ((int) $i) === $i;
// 0.001600980758667 seconds
?>
So if you want the best performance and know you're not going to be passed a string that needs
to be typed as an integer, use === to compare the type as well.
http://php.net/manual/en/function.is-int.php