note 95468 modified in function.is-int by thiago
| From: | thiago@php.net | Date: | Mon, 04 Jan 2010 19:46:36 +0000 |
| Subject: | note 95468 modified in function.is-int by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164636@lists.php.net to get a copy of this message | ||
An interesting thing I discovered is that is_int() may not work if checking a string for only
numeric values.
As a for instance, I have a form that requests user input, and the field name is $search.
When the user clicks the "submit" button, the form then runs through a couple of
verifications, including one that checks whether they entered only numbers in the search (this is
for those who specify searching by only a receipt number), not a hex code or some other
alpha-numeric term.
When running ...
<?php
if (is_int($search)) {echo "Say something nice.";
}else{
echo "Oops! You're no mathematician!";}
?>
... the script did not work. It read all entries, whether purely numeric or alpha-numeric, as
non-integers.
In this case, "is_numeric" is precisely what is needed to verify whether the search is
only a number. So the script becomes ...
<?php
if (is_numeric($search)) {echo "Say something nice.";
}else{
echo "Oops! You're no mathematician!";}
?>
Just a noob's 'aha moment'.
--was--
An interesting thing I discovered is that is_int() may not work if checking a string for only
numeric values.
As a for instance, I have a form that requests user input, and the field name is $search.
When the user clicks the "submit" button, the form then runs through a couple of
verifications, including one that checks whether they entered only numbers in the search (this is
for those who specify searching by only a receipt number), not a hex code or some other
alpha-numeric term.
When running ...
if (is_int($search)) {echo "Say something nice.";
}else{
echo "Oops! You're no mathematician!";}
... the script did not work. It read all entries, whether purely numeric or alpha-numeric, as
non-integers.
In this case, "is_numeric" is precisely what is needed to verify whether the search is
only a number. So the script becomes ...
if (is_numeric($search)) {echo "Say something nice.";
}else{
echo "Oops! You're no mathematician!";}
Just a noob's 'aha moment'.
http://php.net/manual/en/function.is-int.php