is_numeric - not sure why its not working


Im slowly trying to teach myself php in my spare time however I need to get the is_numeric function working for my employers website.

In order to understand it I have been playing with very basic code, as far as I can tell it should work but its not.

Can anyone shed any light on why not??

[code] Calculation Form

Value 1:
Value 2:



PHP Test <? if(is_numeric($myNumber)) echo "The $myNumber variable is numeric
"; else echo "The $myNumber variable is not numeric
"; if(is_numeric($myString)) echo "The $myString variable is numeric
"; else echo "The $myString variable is NOT numeric
"; ?> [/code]

When I run the html form and enter numeric values it still returns them as non numeric ?

Any help appreciated !



Is there a particular reason that you have, in your form, the Value2 Item named $myString (note the $)?

I am not sure, in this instance, but in many languages, that’s akin to casting it as a string so it would ALWAYS be a string value (and you would need to cast it back to an int or float to do math with it.)

If you remove that $ in the form page, it works flawlessly for me

I beg to differ :wink:

Because the values are sent through a form, the values are POST values, and the values need to be extracted from the $_POST superglobal. The above example only works if register_globals is turned ON, which is by default bad practice.

Getting your values from the form works like this:

[php]$myString = $_POST[‘myString’];[/php]

This is a built-in safety to prevent code injection into PHP. It’s also a threshold to make sure programmers check their user input for (deliberately) incorrect values, before processing it.

Besides is_numeric(), I would also suggest taking a look at gettype().


The $ infront of myString was a a typo :oops:

I’ve simplified the form and php snippet even more now but it still returns the value as non-numeric?

Is there anything else that could be causing it no to work? I’ve tried it on my local php server and on my live website and I get the same problem.

Below is the edited code

html Form



php code


PHP Test <? if(is_numeric($Number)) echo "The $Number variable is numeric
"; else echo "The $Number variable is not numeric
"; ?> [/code]

Looks like my question was answered while I was posting :wink:

Cheers, i’ll give it ago


Sponsor our Newsletter | Privacy Policy | Terms of Service