Validating Numeric Data with in_array() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Validating Numeric Data with in_array() (/showthread.php?tid=40862) |
Validating Numeric Data with in_array() - El Forum - 04-20-2011 [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 Validating Numeric Data with in_array() - El Forum - 04-21-2011 [eluser]InsiteFX[/eluser] Maybe define and set m to a value! It sholud not be enclosed in qoutes also! Validating Numeric Data with in_array() - El Forum - 04-21-2011 [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. Validating Numeric Data with in_array() - El Forum - 04-22-2011 [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. Validating Numeric Data with in_array() - El Forum - 04-22-2011 [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! |