Using multiple OR statements

I am using a PHP file to output data to an HTML form, and to make the PHP file easier to maintain I am trying to use ‘or’ statements (e.g if $test==“1” or “2” or “3” echo…) but every time I do the file outputs data before you actually submit the form. The strange thing is I can use multiple ‘or’ statements in the first ‘if’ string but any after that messes it up.

Hope that makes sense.

Thanks,
-Luke

Show us your code.

The whole file is pretty big but here’s the beginning part where the problem starts…


<?php
if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];

if ($car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="Unopened"){
echo "Your iPhone is worth: $875 - $900";

}

}
if ($car=="ATT" and $col=="Black" and $sze=="16GB" or "8GB" and $sof=="Jailbroken" or "Unlocked" or "Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
echo "Your iPhone is worth: $175 - $190";
}

The first part works just fine, but in the second part if I have a third ‘or’ statement ($sof==“Jailbroken” or “Unlocked” or “Factory”) then it outputs the echo before someone actually submits the form. However, if you only have $sof==“Jailbroken” or “Unlocked” then its OK.

$sze==“16GB” or “8GB”

someone correct me if I am wrong, but you can’t do this

you need
$sze==“16GB” or $sze==“8GB”

if you don’t compare “8GB” to something it will return true and you have this 3 times in your if

it is like saying

if(1){
   echo "Something";
else
   echo "Other Thing";
}

the else will not execute because it is always true

I would also simplify the ifs

<?php 
if(isset($_POST['carrier' and 'color' and 'size' and 'software' and 'model' and 'condition'])){ 
$car=$_POST['carrier']; 
$col=$_POST['color']; 
$sze=$_POST['size']; 
$sof=$_POST['software']; 
$mod=$_POST['model']; 
$con=$_POST['condition']; 

if ($car=="ATT" 
     and $col=="Black" 
     and ($sze=="16GB" or $sze=="8GB") 
     and ($sof=="Jailbroken" or $sof=="Unlocked" or $sof=="Factory") 
     and $mod=="iPhone 1st Generation" )
{
  if ($con=="Unopened")
  { 
    echo "Your iPhone is worth: $875 - $900"; 
  }else if ($con=="New"){
    echo "Your iPhone is worth: $175 - $190";
  }
} 

Yeah… think i know what’s going on… try using && for and and || for or.

EDIT : And that giant and at the top wont work. You’ve gotta check each individual one. (isset($_POST[‘value1’]) && isset($_POST[‘value2’]) etc…)

Shouldn’t this kind of thing be checked against a database of values, or at least a csv file?

OK first off I fixed the isset at the top, second I changed all ands to && but when changing the ors to II the form wouldn’t even load. However, doing what craqgerbill said and using


if $test=="value1" or $test=="value" 

so far has fixed the problem.

I’ll report back if I find anything else wrong.
Thanks

I was originally going to use a MySQL database instead of housing all the values in a single PHP file but I don’t know practically anything when it comes to SQL =P

Most of us started the same as you and there are plenty of easy to understand Mysql tutorials out there.

It’s not II, it’s || (on US keyboards, hold shift and push the \ key).

Be very careful using ‘or’. As has been discussed before, the precedence order of ‘or’ and ‘and’ can lead to some unexpected results…

I’m on a Mac so it doesn’t have that (or maybe it does and I don’t know the command). I am using && instead of ‘and’ now but do you think I’ll run into problems because I’m still using ‘or’?

Well, like is pointed out in that thread… if you ever do something like

$f = false or true;
… $f’s value is False.

So Steve went as far as inventing his own keyboard…
Well, as a dutch saying goes: who wants to be beautiful has to suffer :smiley:

Googling for ‘pipeline on mac keyboard’ gave me this: http://en.kioskea.net/faq/1593-making-pipe-under-mac
(the | is called pipeline)

&&
||

Just copy and paste those for now then. :slight_smile:

The weird thing is now that I’m using && and || everything in the entire PHP file gets outputted now matter what is selected. But when I use ‘and’ + || it works properly? Here’s a little test file below…


<?php
if(isset($_POST['carrier']) and isset($_POST['color']) and isset($_POST['size']) and isset($_POST['software']) and isset($_POST['model']) and isset($_POST['condition'])) {
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];

if ($car=="ATT" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 4S" and $con=="Unopened"){
echo "Your iPhone is worth:";

}

}
if ($car=="Sprint" and $col=="Black" and $sze=="16GB" and $sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory" and $mod=="iPhone 1st Generation" and $con=="New"){
echo "Your iPhone is worth:";
}

?>

When using ‘and’ in-place of && and || in place of ‘or’ everything works properly, but as soon as I change ‘and’ to && everything in the file will be outputted.

When mixing && and || you should put use brackets to avoid any unwanted behaviour:


if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory") && $mod=="iPhone 4S" && $con=="Unopened") {
  echo "Your iPhone is worth:";
}

assuming that this is the way you want it to behave

Ok, I must have screwed something up because nothing works now. I threw together this test file but I can’t seem to get it to work! Would anyone mind checking over it because I can’t seem to find the problem:


<?php
if(isset($_POST['carrier']) && isset($_POST['color']) && isset($_POST['size']) && isset($_POST['software']) && isset($_POST['model']) && isset($_POST['condition'])) {
$car=$_POST['carrier'];
$col=$_POST['color'];
$sze=$_POST['size'];
$sof=$_POST['software'];
$mod=$_POST['model'];
$con=$_POST['condition'];

if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || $sof=="Unlocked" || $sof=="Factory") && $mod=="iPhone 4S" && $con=="Unopened"){
echo "Your iPhone is worth: output1";

}

}
if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || sof=="Unlocked" || sof=="Factory") && $mod=="iPhone 4S" && $con=="Like New"){
echo "Your iPhone is worth: output2";
}
if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || sof=="Unlocked" || sof=="Factory") && $mod=="iPhone 4S" && $con=="Good Condition"){
echo "Your iPhone is worth: output3";
}



?>

EDIT: I removed the multiple $sof==“” so it now looks like this

 if ($car=="ATT" && $col=="Black" && $sze=="16GB" && ($sof=="Jailbroken" || "Unlocked" || "Factory") && $mod=="iPhone 4S" && $con=="Good Condition"){
echo "Your iPhone is worth: output3"; }

And everything works properly now, but am I going to run into future problems by not defining each with $sof==“” or does that not matter because of the ()?

I think, where you have,
}

}

you should have only one }
and that there should be another } at the very end, after the very last }

PS whenever you think of using more than one AND and/or OR, use ( ) to help control the logic. Replying on the rules of precedence to get things right is always risky, ( ) always works.

($sof=="Jailbroken" || [B][COLOR="#FF0000"]sof[/COLOR][/B]=="Unlocked" || [B][COLOR="#FF0000"]sof[/COLOR][/B]=="Factory")

See what is missing? The same mistake in the last if as well.