Solved

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

Posted on 2014-03-18
9
196 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
I swear kaufmed... your answer was not there when I started typing. :)
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 74

Expert Comment

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

Author Comment

by:TechIsaac
Comment Utility
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
Comment Utility
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
Comment Utility
Hey Guys,

Thanks so much for help.

Things worked well thanks to your support.

Isaac
0
 

Author Closing Comment

by:TechIsaac
Comment Utility
Real experts are abroad here.

Isaac
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now