Using php if statement to check input value is between 0 and 100

I tried the following to make sure the input value is between 0

and 100

, but it doesn't work.

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

      

+3


source to share


4 answers


Problem 1: string

vsint

You are currently comparing strings, not numbers. Inject the variable $_POST

in int

and remove the apostrophes around 0

and 100

to fix this:

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

      

Problem 2: something is missing

However, this will still not give the results you want as you are missing enough $rate

for the second comparison. Change it to:

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

      

Problem 3: or

vsand

And we're still not there. You are currently using ||

(or)
. Instead, you need to use &&

(and)
:

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

      

Problem 4: =<

vs<=

Still won't work. One of your comparison operators is in the wrong way. =<

should be <=

instead of:



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

      

Problem 5: user input

$_POST

contains data coming from the user. Never trust user data. For example, what if there is no item rate

? In this case PHP throws a type error E_NOTICE

(which you don't even see if you don't have a set of reports before E_ALL

) and after converting to int

, appreciate it 0

! Hence, your test will pass, which is probably not what you want. To prevent this, use isset()

:

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

      

Problem 6: something esle

As pointed out by Pedro Lobito , you also managed to enter a typo: esle

should be else

.

Problem 7: semicolon

As pointed out by Gert your line is $rate = $_POST['rate']

missing ;

at the end.

Summary

I don't mean to be rude, but seeing how many bugs you've managed to compress into this simple one if

, I feel like you should probably consult some basic PHP tutorials instead of Stack Overflow at this point.

+5


source


Several problems with your code,

1 - This is string :, '100'

this is integer :, 100

you cannot check if the number is greater than a string, or at least it doesn't make sense.
2 - Correct syntax of comparison operator : >=

NOT =>


3 - You have a typo in esle

, it must beelse


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 can read about php

Logical Operators ( and

, or

etc.)

+1


source


format it 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>';
          }

      

0


source


To check a number between 0 and 100 try this,

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

      

0


source







All Articles