Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda