chiprivers Posted April 15, 2007 Share Posted April 15, 2007 What would be the correct syntax be to check if a given variable is not numeric? Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/ Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 <?php $var = 'abc'; if (!is_numeric($var)) echo "Not numeric" ?> Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229618 Share on other sites More sharing options...
chiprivers Posted April 15, 2007 Author Share Posted April 15, 2007 <?php $var = 'abc'; if (!is_numeric($var)) echo "Not numeric" ?> Will this also work for: $var = '123'; as apposed to: $var = 123; Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229619 Share on other sites More sharing options...
irken Posted April 15, 2007 Share Posted April 15, 2007 Well. Doing, as per Barand's snippet: $numer = '12345678'; if (!is_numeric($number)) echo "Not numeric"; Does actually not output "Not nummeric". I thought that ' (pings) got treatet as a string? Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229621 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 '123' is numeric <?php $var = '123'; if (is_string($var)) echo "a string "; if (!is_numeric($var)) echo "Not numeric"; else echo "Numeric"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229622 Share on other sites More sharing options...
irken Posted April 15, 2007 Share Posted April 15, 2007 Yes. But isn't that a bit weird.. why is something enclosed in pings treatet as a numeric? Maybe I got the whole what's a string and what's not wrong : ). You learn every day - thanks! Edit: Perhaps it's safe to use is_int() on the $var instead? If you're just checking for 12345.. Edit again again: Oh, I just read the manual. "To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric()." - that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229624 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 PHP is a typeless language, so string are automatically converted to numeric if required <?php $var = '123'; echo $var * 10; //--> 1230 $var = '1,230'; echo $var * 10; //--> 10 // the numeric value of var is 1, as the next char is non-numeric ?> Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229626 Share on other sites More sharing options...
wildteen88 Posted April 15, 2007 Share Posted April 15, 2007 is_numeric checks whether the given variables holds a numeric value, weather a number is held within a string or not. The following is considered numeric: '123' "123" 123 -123 1.23 If want to check that the variable holds an integer, then use is_int. is_int considers the following as integers: 123 -123 Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229630 Share on other sites More sharing options...
chiprivers Posted April 15, 2007 Author Share Posted April 15, 2007 I'm a bit lost now, let me explain in what context I am using this and perhaps you could explain my options? I am dealing with horse racing results and I am extracting the data from the code source of a webpage. The section that lists the horses gives their finishing position which is obviously a numeric value. I am inserting this into a mysql database with a column type int. However, for some races, horses are also listed that did not complete the race and the value returned where there would normally be a numeric value is an abbreviation of letters indicating the reason that they did not finish the race, ie PU for pulled up. This value I can obviously not put into my current int column in the database. So I wanted to have a seperate varchar column in the database where I put an entry if the horse did not complete the race, hence I need to check whether the value taken from the script is numeric or not to know which column to put it into. Perhaps I am making this a lot more complex than I need to! Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229634 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 perhaps <?php $data = 'PU'; $position = is_numeric($data) ? $data : 0; $reason = is_numeric($data) ? '' : $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229657 Share on other sites More sharing options...
ted_chou12 Posted April 15, 2007 Share Posted April 15, 2007 I think you are looking for this: if (is_int($variable) == false) { Quote Link to comment https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.