Validating Numeric Data with in_array() |
[eluser]Louis C.[/eluser]
I'd like to use in_array to determine to confirm that some data is numerical before I do things with it. But in_array() is confusing me with its output: Code: ➜ ~ php -r 'print in_array(9, range(0, 10));' Why does in_array say that both m and 'm' are contained in my numeric range? What is the best way to determine if $number is between 1 and 10 in PHP? Thanks, Lou
[eluser]InsiteFX[/eluser]
Maybe define and set m to a value! It sholud not be enclosed in qoutes also!
[eluser]Louis C.[/eluser]
[quote author="InsiteFX" date="1303414379"]Maybe define and set m to a value! It sholud not be enclosed in qoutes also![/quote] Thanks for the suggestion and advice. I'm relatively new to PHP. I think I'll just need to verify the type of the variable with is_numeric() before using in_array() to search the array for it.
[eluser]danmontgomery[/eluser]
[quote author="Louis C." date="1303372607"]I'd like to use in_array to determine to confirm that some data is numerical before I do things with it. But in_array() is confusing me with its output: Code: ➜ ~ php -r 'print in_array(9, range(0, 10));' Why does in_array say that both m and 'm' are contained in my numeric range? What is the best way to determine if $number is between 1 and 10 in PHP? Thanks, Lou[/quote] Because your syntax is wrong. You're doing: Code: print in_array(m, range(0, 10)); in both lines. As the error says, the undefined constant "m" is interpreted as a string. The integer value of "m" is 0, which is contained in the array. http://php.net/manual/en/function.in-array.php Quote:If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
[eluser]Louis C.[/eluser]
noctrum, that is exactly what I was looking for. I just didn't read the docs hard enough. Thanks so much! |
Welcome Guest, Not a member yet? Register Sign In |