N.Ali
N.Ali

Reputation: 61

Using php if statement to validate that the input value is between 0 and 100

I tried the following to make sure the input value is between 0 and 100 but it didn't work.

  if (($_POST['rate']) >= '0' || =<'100') {
      $rate = $_POST['rate']
  }
  esle {
      echo '<p>Please enter a value between 0 and 100</p>';
  }

Upvotes: 3

Views: 1978

Answers (4)

domsson
domsson

Reputation: 4691

Issue 1: string vs int

Currently, you are comparing strings, not numbers. Cast your $_POST variable to int and remove the apostrophes around 0 and 100 to fix this:

$rate = (int) $_POST['rate'];
if ($rate >= 0 || =< 100) { /* ... */ }

Issue 2: something is missing

However, this is still not going to yield the desired results as you are missing $rate for the second comparison. Change it to:

$rate = (int) $_POST['rate'];
if ($rate >= 0 || $rate =< 100) { /* ... */ }

Issue 3: or vs and

And we're still not there. Currently, you are using || (or). You need to use && (and) instead:

$rate = (int) $_POST['rate'];
if ($rate >= 0 && $rate =< 100) { /* ... */ }

Issue 4: =< vs <=

Still not going to work. One of your comparison operators is the wrong way round. =< should be <= instead:

$rate = (int) $_POST['rate'];
if ($rate >= 0 && $rate <= 100) { /* ... */ }

Issue 5: user input

$_POST holds data coming from a user. Never trust user data. What, for example, if there is no rate element? In that case, PHP will throw an error of type E_NOTICE (which you might not even see if you don't have your reporting set to E_ALL) and once converted to int, evaluate to 0! Hence, your test would pass, which is probably not what you want. To prevent that, use isset():

$rate = isset($_POST['rate']) ? (int) $_POST['rate'] : -1;
if ($rate >= 0 && $rate <= 100) { /* ... */ }

Issue 6: something esle

As pointed out by Pedro Lobito, you managed to also introduce a typo: esle should be else.

Issue 7: semicolon

As pointed out by Gert, your line $rate = $_POST['rate'] is missing the ; at the end.

Summary

I don't mean to be rude, but seeing how many mistakes you managed to squeeze into this simple if, I get the feeling that you should probably consult some basic PHP tutorials instead of Stack Overflow at this point.

Upvotes: 6

Pedro Lobito
Pedro Lobito

Reputation: 98881

Several issues with your code,

1 - This is a string: '100', this is an integer: 100, you cannot check if a number is bigger than a string, or at least, it doesn't make sense.
2 - The correct syntax of the comparasion operator is: >= NOT =>
3 - You've a typo in esle, it should be else


Based on the above, you need something like:

if(isset($_POST['rate']))
{
    $rate = $_POST['rate'];
    if($rate >= 0 and $rate <= 100)
    {
        echo "rate is between 0 and 100";
    }
}

Note:

You may want to read about php Logical Operators (and, or, etc..)

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

To test number is between 0 and 100, try this,

if($_POST['rate'] >= 0 && $_POST['rate'] <= 100)

Upvotes: 0

Gert
Gert

Reputation: 370

format it like this to check between numbers

$temprate=$_POST['rate'];
          if(($temprate >= '0') && ($temprate <= '100')){
             //you forgot the semi colon here by the way
             $rate = $temprate;
          }
          esle{
             echo '<p>Please enter a value between 0 and 100</p>';
          }

Upvotes: 0

Related Questions