0

a simple question

what is the differences between this:

if($posted_quantity <= $current_quantity) {
    if($posted_quantity >= $min_allowed_quantity && $posted_quantity <= $max_allowed_quantity){
        //do something
    }
}

and this:

if($posted_quantity <= $current_quantity && ($posted_quantity >= $min_allowed_quantity && $posted_quantity <= $max_allowed_quantity)){
    //do something
}

if it is possible, what is the differences between those two, which one is better to use, thanks much

EDIT:i am currently doing the testings and i don't seem to find any problem using both approach, and i don't have any elses, i simply want to do something once the whole conditions is fulfilled (to limit the user's quantity), i was just curious on which one is the better approach, which one is faster, readable, things like that

7
  • 2
    You don't have PHP near you to just try it? I case you really have not, you can test such simple things yourself with codepad.org Commented Jan 31, 2011 at 9:59
  • it seems working on my current testings, i was just curious Commented Jan 31, 2011 at 10:03
  • @Felix Kling - i think littlechad came here to ask for suggestion and our/your advice! And question isnt What is this!?! He knows what is that and where to use it! Commented Jan 31, 2011 at 10:05
  • @littlechad: Then you should clarify that. If I read your question as it is now, it seems to me you are too lazy to run the code yourself... sorry for that but I can't help it. Commented Jan 31, 2011 at 10:05
  • 1
    ok @felix kling, i'll clarify my question, sorry for the misunderstanding Commented Jan 31, 2011 at 10:07

9 Answers 9

1

I you don't have any else or elseif clauses in there, it is exactly the same. Though I assume that your original condition was already wrong:

$posted_quantity <= $min_allowed_quantity && $posted_quantity >= $max_allowed_quantity

This is true if $posted_quanitiy is outside the the min..max range.

Sign up to request clarification or add additional context in comments.

Comments

1

It is indeed possible.

Functionally, they're equivalent, so the difference lies in readability, so go with whichever one you find the most easy to understand.

Personally I'd do something along the lines of

if ($posted_quantity <= $current_quantity &&
    $posted_quantity <= $min_allowed_quantity &&
    $posted_quantity >= $max_allowed_quantity) {
    //do something
}

in order to make it more readable.

Comments

1

Both Style of if Conditions are valid it doesn't seem any difference better to use it in a single condition statement

Hope it Helps!!

Comments

0

It's the same. The advantages of the first is being able to set an else clause for the second if, and readability.

Comments

0

They do the same thing, in the same manner. In both cases if the first condition returns false the second one is not evaluated.

Comments

0

If you don't have any 'else' clause, your first if is useless. Imo, the second is maybe a bit faster, since php doesn't have to parse 2 'if'. But I'm not sure.

Comments

0

The first allows you to have seperate else clauses for each evaluated statement. You may wish to have different things happen if the first staement is false than if the second is false.

Comments

0

Both are the same. i think so Its your programming skill which one use depend on you(batter feeling for you and other easily understanding this code)

Comments

0

ofc. anything is possible! ;)

if($posted_quantity <= $current_quantity && ($posted_quantity <= $min_allowed_quantity && $posted_quantity >= $max_allowed_quantity)){
  //do something
}

Its same if you always need to check for all of that! But if you need to check like this:

if($posted_quantity <= $current_quantity){
  if($posted_quantity <= $min_allowed_quantity){
        //do something
  }
  elseif($posted_quantity >= $max_allowed_quantity){
        //do something else
  }
}

then it would go separated! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.