简体   繁体   English

PHP if子句基本问题

[英]PHP if clause basic question

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 编辑 :我目前正在测试中,使用这两种方法我似乎都没有发现任何问题,而且我没有其他问题,只要满足所有条件(限制用户数量),我只是想做点什么,我只是好奇哪种方法更好,哪种方法更快,更易读,

Both Style of if Conditions are valid it doesn't seem any difference better to use it in a single condition statement 两种Style of if条件均有效,在单个条件语句中使用它似乎没有什么区别

Hope it Helps!! 希望能帮助到你!!

I you don't have any else or elseif clauses in there, it is exactly the same. 我在那里没有elseelseif子句,它是完全一样的。 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. 如果$posted_quanitiy $posted_quanitiy范围之外 ,则为true。

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. 为了使其更具可读性。

It's the same. 一样的。 The advantages of the first is being able to set an else clause for the second if, and readability. 第一个的优点是能够为第二个if和可读性设置else子句。

They do the same thing, in the same manner. 他们以相同的方式做同样的事情。 In both cases if the first condition returns false the second one is not evaluated. 在这两种情况下,如果第一个条件返回false,则不评估第二个条件。

If you don't have any 'else' clause, your first if is useless. 如果您没有任何'else'子句,则第一个if子句是无用的。 Imo, the second is maybe a bit faster, since php doesn't have to parse 2 'if'. Imo,第二个也许更快一些,因为php不必解析2个'if'。 But I'm not sure. 但是我不确定。

The first allows you to have seperate else clauses for each evaluated statement. 第一个允许您为每个求值语句使用单独的else子句。 You may wish to have different things happen if the first staement is false than if the second is false. 如果第一个要素为假,则第二个要素为假,您可能希望发生不同的事情。

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) 我认为这是您的编程技能,取决于您的使用(对您的感觉以及其他容易理解此代码的感觉)

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! 那么它将分开! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM