How to make an if with a higher and a lower value?

Asked

Viewed 1,738 times

-2

I was wondering if it’s possible to make a if or a for in PHP as follows:

$valor = 7
if ( $valor se o valor for maior que > 5 menor que < 10) {
  //... Comando a ser executado ... 
}else if (se o valor foi maior que > 10 é menor que < 15){
  //... Comando a ser executado 
}else {
 // idade inferior a 5 anos 
 }
  • 2

    Yes, it is possible. You have studied PHP operators?

  • very simple if ($value > 5 && $value < 10), but this is the basics recommend studying the basics.

  • Wow, so simple thing I thought I’d do that.. But I didn’t, brigade bro kkk

  • Maybe you don’t know but the PHP has a very complete official documentation. For example the explanation of how the condition works if:http://php.net/manual/en/control-structures.if.php

1 answer

5


The first step is to define well what your problem is. Even in English it is not well defined. One thing is for, another is a if, another is a if and then else if, And even if this is less relevant, the factor of describing the operator in full and using it symbolically makes the text even more confusing, including because the text is not well connected, and linking is the key to the solution you want. I think I understand what you want, but how confused you are may be that my understanding is wrong. I will try to answer what I understood based on the comment.

Would this be:

if ($valor > 5 && $valor < 10) {
    //... Comando a ser executado ... 
} else if ($valor > 10 && $valor < 15) {
    //... Comando a ser executado 
} else {
    // idade inferior a 5 anos 
}

I used the operator of && (in PHP) to concatenate two relational conditions so that both must be true to enter the block.

But there are some problems in this code.

If the age is 10 he will say that the age is less than 5. If the age is greater than or equal to 15 he will say that it is less than 5. Then the correct would be something like this (I had to interpret what I think is right, because even in Portuguese it is not possible to know what is the requirement, I do not know what is the correct formula to be applied.

if ($valor >= 15) {
    //... trata para idade maior ou igual a 15 ... 
} else if ($valor >= 10 && $valor < 15) {
    //... trata para idade entre 10 e 14 anos ...
} else if ($valor >= 5 && $valor < 10) {
    //... trata para idade entre 5 e 9 anos ...
} else {
    // ... trata para idade inferior a 5 anos ...
}

I put in the Github for future reference.

First of all programming is to understand the problem and find a suitable solution. Coding is a silly detail that we have to do. Learning syntax is not learning to program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.