Jump to content

if ($variable != int) ...


chiprivers

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229624
Share on other sites

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

?>

Link to comment
https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229626
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229630
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/47077-if-variable-int/#findComment-229634
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.