0

I have a textbox that is an optional field for users to enter a number in to. If there is a number in there, I want to check to make sure it is less than 500 and then do something with it.

Here is what I am currently doing:

if($textbox!="" && <=500)
{
//action here
}

I tried replacing && with andif but still get an error Parse error: syntax error, unexpected T_IS_SMALLER_OR_EQUAL

What's the easiest way to do this?

2 Answers 2

4

You need to use the variable in both statements preg_match('/\d/', $textbox ) == 1 will make sure it's an int

if($textbox!="" && $textbox <= 500 && preg_match('/\d/', $textbox ) == 1)
{
//action here
}
2
  • 1
    Bringing regex into this is overkill. Personally, I'd recommend going with: if (!empty($textbox) && intval($textbox) <= 500) { /* CODE */ }
    – Mr. Llama
    Commented Apr 13, 2012 at 20:54
  • 2
    Yeah, I noticed that I forgot to put an is_numeric in there but it was already too late to edit.
    – Mr. Llama
    Commented Apr 13, 2012 at 21:14
0

you are missing the left part of the lesser than

if($textbox!="" && $textbox <=500)
{
//action here
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.