Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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>';
  }
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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..)

about 4 years ago · Santiago Trujillo Report

0

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>';
          }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!