note 90198 modified in function.is-int by felipe

From: Date: Sun, 12 Apr 2009 20:00:19 +0000
Subject: note 90198 modified in function.is-int by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153124@lists.php.net to get a copy of this message
Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need). He made some errors on his ctype_digit() output though - possibly a typo, or maybe a bug in his version of PHP at the time. The correct output for parts of his examples should be: <?php var_dump(ctype_digit(23)); //bool(false) var_dump(ctype_digit("23")); //bool(true) var_dump(ctype_digit(23.5)); //bool(false) var_dump(ctype_digit(NULL)); //bool(false) var_dump(ctype_digit("")); //bool(false) ?> As you can see, the reason why using *just* ctype_digit() may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE (which may be unexpected). --was-- Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need). He made some errors on his ctype_digit() output though - possibly a typo, or maybe a bug in his version of PHP at the time. The correct output for parts of his examples should be: var_dump(ctype_digit(23)); //bool(false) var_dump(ctype_digit("23")); //bool(true) var_dump(ctype_digit(23.5)); //bool(false) var_dump(ctype_digit(NULL)); //bool(false) var_dump(ctype_digit("")); //bool(false) As you can see, the reason why using *just* ctype_digit() may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE (which may be unexpected). http://php.net/manual/en/function.is-int.php

« previous php.notes (#153124) next »