Solved

Long compound conditional - How to make it evaluate to true when all true..php5

Posted on 2014-03-18
9
200 Views
Last Modified: 2014-03-21
How do I make sure this whole statement evaluates to true when true.

That is, I want the test for true to capture all elements.  Is There a rule for parenthesis?

(((isset($_POST['one']) && ($csv_column_values[1] == "1") && ($csv_column_values[0] >= $minprice) && ($csv_column_values[0] <= $maxprice) && ($csv_column_values[2] == "Y") && ($maxprice <= 1400))))

Isaac
0
Comment
Question by:TechIsaac
  • 3
  • 3
  • 3
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39937988
If all of the conditions are AND conditions (or all are OR conditions), then you don't need parentheses. Functionally speaking, parentheses only help when you are mixing AND and OR conditions. You might consider putting each condition on its own line for readability's sake.

e.g.

if (isset($_POST['one']) &&
    $csv_column_values[1] == "1" &&
    $csv_column_values[0] >= $minprice &&
    $csv_column_values[0] <= $maxprice &&
    $csv_column_values[2] == "Y" &&
    $maxprice <= 1400) {
    
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39938022
Also keep in mind that PHP, like many other languages, offers short-circuit evaluation. If PHP can determine from any subset of conditions that the overall condition will fail, it doesn't have to examine every condition. What I mean is, if your conditions basically came down to this:

if (true &&
    false &&
    true) {

}

...PHP doesn't have to check the third condition since the second was is false. In Boolean algebra, ANDing a true and a false will result in a result of false. If at least one of the conditions in an AND chain will be false, then the overall result of the whole AND chain will be false. PHP can skip the remaining conditional checks once it finds a single false. It will, however, have to continue checking the remaining conditions as it finds true results.

For OR conditions, it's kind of the opposite:  Any true that is found will result in the overall condition being true. In other words:

if (false ||
    true ||
    false) {

}

...PHP doesn't have to check the third condition because it found a true in the second condition. Any single true will result in the whole OR chain being true.

When you mix AND and OR, you still have the same rules, but AND binds tighter (has higher precedence) than OR, so you have to include parentheses where you want the OR to have higher precedence.

e.g.

if (true &&
    (true || false) &&
    true) {

}

You might want to have a read about truth tables.
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39938025
Looks to me like your entire expression could have all the parenthesis removed and evaluated for true of false.

The && between each conditional will guarantee that the expression in it's entirety evaluates to true.  That is the definition of the Logical and "&&".

The only thing you would have to be careful of are any items that have a lower precendence that && in your expressions.

(See this for complete list : http://www.php.net/manual/en/language.operators.precedence.php)

if you replace the following "expression" with your expression listed in your post,
if (expression){
}

Open in new window

this would work:
if (isset($_POST['one']) && $csv_column_values[1] == "1" && $csv_column_values[0] >= $minprice && $csv_column_values[0] <= $maxprice && $csv_column_values[2] == "Y" && $maxprice <= 1400) {
}

Open in new window

Howver just because you can do something does not necessarily mean you should.  I think for both readability and maintainability, I would use parens around each subclause something like this:
if (
    (isset($_POST['one'])) && 
    ($csv_column_values[1] == "1") && 
    ($csv_column_values[0] >= $minprice) && 
    ($csv_column_values[0] <= $maxprice) && 
    ($csv_column_values[2] == "Y") && 
    ($maxprice <= 1400)
    )  {
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Ken Butters
ID: 39938031
I swear kaufmed... your answer was not there when I started typing. :)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39938038
It's all just a dream.....   *waves fingers*   OOooooooooooohhhh....
0
 

Author Comment

by:TechIsaac
ID: 39941467
Thanks. my code looks much cleaner and values seem to evaluate to true.

The problem is that I thought this was the reason I could not do more than one
redirect in my script.  It seems no matter the condition, I can only do the first
redirect in the script ;
The script is used here:
http://124marketplace.com/floorplans/floorplanselector.html

If a check one and select maximum price range to 1400,1500,1600 they all select the
same display although 1400 and 1500 should show the same display.  Given all the
condition are true for 1600 it should show 2 1 bedroom layouts.

These are the selections that I have being making changes to in order two cure the
problem.

Thanks,
Isaac
holdfile.zip
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39942311
It looks like the conditional logic is not your issue.  You need to add some var_dump instructions to your code to help you figure out which items are unexpectedly evaluating to false.

For your items that are not variables exactly, but function calls...  like :  isset($_POST['one'])... i'd assign that to a boolean var, and then var_dump that boolean...

In the end... I am thinking that something in that expression is evaluating to false, when you expect it to evaluate to true.
0
 

Author Comment

by:TechIsaac
ID: 39946825
Hey Guys,

Thanks so much for help.

Things worked well thanks to your support.

Isaac
0
 

Author Closing Comment

by:TechIsaac
ID: 39946827
Real experts are abroad here.

Isaac
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
countEvens challenge 2 113
wordlen challenge 3 80
wordmultiple challenge 12 154
Problem to page 4 99
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question