Php <= "value" and >= "value"?

Asked

Viewed 1,939 times

0

I would like to know how to make 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 make it include a file in the content and I need PHP to detect whether these 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 which has two values greater than value and less than value has given me trouble, already put &, && and and and it doesn’t work out that PHP detects if a variable has a value between 40 and 99 to include M1.html.

  • 1

    You are using strings! You need to use numbers, or it will compare how many characters you have instead of the numeric value...

  • In this case the value of var is type number to var is a java script in 1° and 3° if you want to remove 2° if now when I try to check if the value is between 40 and 99 of the error, I wanted to know how to put to check if this var is between 40 and 99... @Sergio

  • Instead of $var <= "100" you have to take out the quotes and have $var <= 100. Makes sense the difference?

  • It does, but the worst is that while we have two values <= and => wrong. Even if quotation marks This code snippet of the if error ( $var <= "40" && >= "99")

  • Can’t be a <= b and b => c at the same time. In other words, it is a case, when a == c.

  • Do not have anything to check if var this among these values???

  • Can you make a table with the cases you want/have? so we can help more in logic and code...

Show 2 more comments

2 answers

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
}
?>

2


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

First you should review your logic. The third if for example limit 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 trouble with logic, post it.

  • You see, the way you think it seems to work, but you can’t do it that way. PHP is interpreted on the server, and JS in the browser, so it will be interpreted after PHP. To pass a JS parameter to PHP you will need to re-load the page and recover the value via $_GET in PHP. You can use window.location.repalce("suapagina.php?numero=" + wi); Then you recover the number in PHP: $var = $_GET["numero"];

  • Got it and Sorry I haven’t learned yet to give this gray background in the code in comments

  • Yes @qmechanik is that I pull the value of <script> var wi = screen.width;</script> so later in php I want to return to a specific html according to the resolution

  • Just making a correction in my comment: The javascript code would be: window.location.replace("suapagina.php?numero=" + wi); had a little mistake, because I made it right here :)

  • Is it possible to pass $_POST in PHP after all I have to run this function 2 or 3 times on the same page

  • This kind of thing I don’t think would be cool to go through $_POST because if the user updated the page would keep asking for confirmation. But what you could do and it’s very common would be to do the calculation directly in javascript and pass the name of the page through $_GET, so the url wouldn’t be weird so to say. Your url would look like: suapagina.php? page=m3. You can customize the name and treat it in php if you want, then it’s up to you.

  • Vlw worked well I created a page that takes the value and then plays for all Divs ! It made my life a lot easier.

Show 2 more comments

Browser other questions tagged

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