Php="value" e="value"?

0

I would like to know how to do PHP detect if a variable is with the value between one of the values passed.

Explaining better I have a variable with a value, this value will cause it to include a file in the content and I need PHP to detect if this value is greater than or equal to "value" and less than or equal to "value."

<?php 
$var = "valor>";

if ( $var <= "100") 
     include "m3.html";
else{
     if ( $var <= "40" && >= "99") 
          include "m1.html";
     else{
         if ( $var >= "39")
              include "m2.html";
    }
}
?>

As you can see the second if that has two values greater than value and less equal to value has given me problem, I already put & && and and and it does not make it necessary for PHP to detect if a variable has a value between 40 and 99 to include m1.html in>.

    
asked by anonymous 31.05.2015 / 20:46

2 answers

2

Friend your code will always be in the first if because to get in else it would have to be greater than 100, and operations within else are all less than 100, understood?

You should first review your logic. The third% w / w of% for example limits you only if the variable is equal to 39, if it does not fall in the second if, which by the way is in error, and the correction would be:

if ( $var >= 40 && $var <= 99 )

If you have difficulty with logic, post it.

    
31.05.2015 / 23:12
2

In the second if there is a syntax error $var <= "40" && >= "99" must be $var >= "40" && $var <= "99" .

<?php 
$var = "valor";
$varInt = (int)$var;  // Faz um cast para inteiro

if ($varInt <= 100) { // Tem certeza que isto aqui está certo? Não seria >= 100? 
   include "m3.html";
}
elseif ( $varInt >= 40 && $varInt <= 99) { // Se for 40 ou maior, e menor ou igual a 99 
   include "m1.html";
}
elseif ($varInt <= 39) { // Se for menor ou igual a 39. No teu código está >= 39
   include "m2.html";
}
else {
    // Fazer algo aqui caso tudo acima não seja executado
}
?>
    
31.05.2015 / 22:51