Tuesday 17 December 2019

php - How to check if a text box is not empty and the value is less than 500



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?


Answer



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
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...