Heatmanofurioso
Heatmanofurioso

Reputation: 1028

PHP If Statement Logic

I am a bit stuck on an if statement on my PHP code. I know this is probably a basic question for most of you out there, but i am stuck, and i would like some assistance.

I have 2 variables $max and $ min, which i have to compare with 2 other max and mins called $valor and $minimo in order to check if they are meeting somewhere. What i basically have is a set of values like min=20 and max=30.

I want to compare those with the other max and min, and check if somewhere the values meet, like if the min of the second one is 29. I want it to enter the if statement.

Here's the statement i got right now. But it's not working, and i just can't get the logic on it. Any help?

EDIT: Added an example of what i am trying to achieve in the comments.

if ($min >= $valor && $min <= $minimo || $max >= $valor && $max <= $minimo)
    {
        //Do nothing
    }
else
    {
        $queryq = "INSERT INTO precos_area (id_tecido,area_minima,area_maxima,preco) VALUES ('".$id_tipo."', '".$min."', '".$max."', '".$price."')";
        $resultsq = mysql_query($queryq) or die(mysql_error());
    }

Upvotes: 1

Views: 105

Answers (3)

Kickstart
Kickstart

Reputation: 21533

I have a feeling the 2nd max and min are the wrong way round

if (($min >= $minimo && $min <= $valor ) || ($max >= $minimo && $max <= $valor ))

EDIT - to catch the situation that gview mentions you probably should check both ranges. This will then catch a situation where one range is completely within another range:-

if (($min >= $minimo && $min <= $valor ) 
|| ($max >= $minimo && $max <= $valor )
|| ($minimo >= $min && $minimo <= $max ) 
|| ($valor >= $min && $valor <= $max ))

Upvotes: 2

Ilanus
Ilanus

Reputation: 6928

I think what you are looking for is something like this:

<?php

$min = 10;
$max = 20;
$valor = 25;
$minimo = 30;

if (($min >= $minimo && $min <= $valor ) || ($max >= $minimo && $max <= $valor )) {
print "win";
}

else {
print "lose";
}

?>

Upvotes: 0

Drop Shadow
Drop Shadow

Reputation: 808

Use

if (($min >= $valor && $min <= $minimo) || ($max >= $valor && $max <= $minimo))

Upvotes: 1

Related Questions