Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda