449,276 Members | 1,245 Online
Need help? Post your question and get tips & solutions from a community of 449,276 IT Pros & Developers. It's quick & easy.

# Nunber check Function failing

 P: n/a The following function checks to see if a variable read from a mysql database is numeric. The funtion worked until I hit the value 15 303 That is a valid number but because of the space between the 15 and the 303 for (fifteen thousand three hundred and three) it errored function is_number(\$number) { \$text = (string)\$number; \$textlen = strlen(\$text); if (\$textlen==0) return 0; for (\$i=0;\$i < \$textlen;\$i++) { \$ch = ord(\$text{\$i}); if ( (\$ch < 48 ) || (\$ch 57) ) return 0; } return 1; } So I replaced the second if row with this one to check for character 32 (space) when less then charcter48 if ( ((\$ch < 48 ) && (\$ch != 32)) || (\$ch 57) ) return 0; But that has not really helped It now gives me this error for 15 303 - Parse error: syntax error, unexpected T_LNUMBER in...(file) on line 104 Line 100-104 in my test are 100 print is_number(545); 101 print is_number(ca100); 102 print is_number( ); 103 print is_number(none); 104 print is_number(15 303); I also need this to identify (ca 100) as being non-numeric it works as above on (ca100) but not with the added space between ca and 100. However line 102 has several spaces and that does not error but gives "Warning: Missing argument for is_number(), called in (file) on line 102 " In gathering future input from users is it possible to restrict the user from being able to type in any other characters than 0-9? I still need to solve the number check as I am using this code to add one to the value if conditions are met. Any help very much appreciated Garry Jones Sweden Sep 28 '07 #1
6 Replies

 P: n/a GarryJones wrote: The following function checks to see if a variable read from a mysql database is numeric. The funtion worked until I hit the value 15 303 That is a valid number but because of the space between the 15 and the 303 for (fifteen thousand three hundred and three) it errored function is_number(\$number) { \$text = (string)\$number; \$textlen = strlen(\$text); if (\$textlen==0) return 0; for (\$i=0;\$i < \$textlen;\$i++) { \$ch = ord(\$text{\$i}); if ( (\$ch < 48 ) || (\$ch 57) ) return 0; } return 1; } So I replaced the second if row with this one to check for character 32 (space) when less then charcter48 if ( ((\$ch < 48 ) && (\$ch != 32)) || (\$ch 57) ) return 0; But that has not really helped It now gives me this error for 15 303 - Parse error: syntax error, unexpected T_LNUMBER in...(file) on line 104 Line 100-104 in my test are 100 print is_number(545); 101 print is_number(ca100); 102 print is_number( ); 103 print is_number(none); 104 print is_number(15 303); I also need this to identify (ca 100) as being non-numeric it works as above on (ca100) but not with the added space between ca and 100. However line 102 has several spaces and that does not error but gives "Warning: Missing argument for is_number(), called in (file) on line 102 " In gathering future input from users is it possible to restrict the user from being able to type in any other characters than 0-9? I still need to solve the number check as I am using this code to add one to the value if conditions are met. Any help very much appreciated Garry Jones Sweden you shouldn't need to write a function to check if something is a number, use the built in PHP commands, or use regex for validation if you want eg. if ( ctype_digit( "100" ) ) { echo "It's numerical, hallelujah"; } else { echo "The devil is in our midst, NaN..."; } However, what you have there really isn't a number "15 303", you would need to replace any spaces or other chars with str_replace/preg_replace prior to a number check, you should validate data first before it gets into your database. Sep 28 '07 #2

 P: n/a Tyno Gendo wrote: >Line 100-104 in my test are100 print is_number(545);101 print is_number(ca100);102 print is_number( );103 print is_number(none);104 print is_number(15 303); You would need quotes around these anyway wouldnt you?... eg. is_number("ca100"); Sep 28 '07 #3