PHP if子句基本问题
一个简单的问题 这两者之间的区别是什么:PHP if子句基本问题,php,if-statement,Php,If Statement,一个简单的问题 这两者之间的区别是什么: if($posted_quantity <= $current_quantity) { if($posted_quantity >= $min_allowed_quantity && $posted_quantity <= $max_allowed_quantity){ //do something } } if($posted_quantity=$min_allowed_quantit
if($posted_quantity <= $current_quantity) {
if($posted_quantity >= $min_allowed_quantity && $posted_quantity <= $max_allowed_quantity){
//do something
}
}
if($posted_quantity=$min_allowed_quantity&&$posted_quantity是相同的。第一个的优点是能够为第二个if设置else子句,并且可读性。是相同的。第一个的优点是能够为第二个if设置else子句,并且可读性。它们以相同的方式做同样的事情在这两种情况下,如果第一个条件返回false,则第二个条件不被计算。它们以相同的方式执行相同的操作。在这两种情况下,如果第一个条件返回false,则第二个条件不被计算。我这里没有任何else
或elseif
子句,但我假设t您的原始状态已经错误:
$posted_quantity <= $min_allowed_quantity && $posted_quantity >= $max_allowed_quantity
$posted\u quantity=$max\u allowed\u quantity
如果$posted\u quantity
在最小.最大范围之外,这是正确的。I您没有任何其他
或其他
子句,这是完全相同的。尽管我假设您的原始条件已经错误:
$posted_quantity <= $min_allowed_quantity && $posted_quantity >= $max_allowed_quantity
$posted\u quantity=$max\u allowed\u quantity
如果$posted\u quantity
在最小.最大范围之外,这是正确的。这确实是可能的
在功能上,它们是等价的,所以区别在于可读性,所以选择你认为最容易理解的
就我个人而言,我会按照
if ($posted_quantity <= $current_quantity &&
$posted_quantity <= $min_allowed_quantity &&
$posted_quantity >= $max_allowed_quantity) {
//do something
}
如果($posted_)数量这确实是可能的
在功能上,它们是等价的,所以区别在于可读性,所以选择你认为最容易理解的
就我个人而言,我会按照
if ($posted_quantity <= $current_quantity &&
$posted_quantity <= $min_allowed_quantity &&
$posted_quantity >= $max_allowed_quantity) {
//do something
}
if($posted\u quantity如果你没有任何“else”子句,那么你的第一个if是无用的。
在我看来,第二个可能要快一点,因为php不必解析2'if'。但我不确定。如果没有任何'else'子句,那么第一个if是无用的。
在我看来,第二个可能要快一点,因为php不必解析2'if'。但我不确定。第一个允许您为每个求值语句使用单独的else子句。如果第一个statement为false,您可能希望发生不同的事情,而不是第二个statement为false。第一个允许您为每个语句使用单独的else子句每个求值语句。如果第一个语句为false,您可能希望发生与第二个语句为false不同的事情。两者都是相同的。
我想这是你的编程技巧,一个人的使用取决于你(击球手对你的感觉和其他容易理解这段代码的人)两者都是一样的。
我想这是你的编程技巧,你可以使用它(对你和其他人的感觉很容易理解这段代码)ofc。任何事情都是可能的!;)
if($posted_quantityofc.一切皆有可能!)
if($posted\u quantity两种类型的if条件都有效,在单个条件语句中使用它似乎没有什么区别
希望对您有所帮助!!如果条件有效,那么在单个条件语句中使用它似乎没有什么区别
希望它能帮上忙!!你身边没有PHP可以直接试用吗?如果你真的没有,你可以自己测试这么简单的东西,它似乎在我当前的测试中发挥作用,我只是curious@Felix克林-我想利特尔哈德是来征求建议和我们/你们的建议的!问题不是这是什么!?!他知道那是什么,什么时候We to use it!@littlechad:那么你应该澄清一下。如果我现在这样看你的问题,我觉得你太懒了,不想自己运行代码……很抱歉,但我无能为力。好的@felix kling,我会澄清我的问题,很抱歉误解,你附近没有PHP可以尝试吗?如果你真的没有,你可以测试su你自己做一些简单的事情,它似乎在我目前的测试中发挥作用,我只是curious@Felix克林-我想利特尔哈德是来征求建议和我们/你的建议的!问题不是这是什么!?!他知道那是什么以及在哪里使用它!@利特尔哈德:那么你应该澄清一下。如果我现在这样读你的问题,在我看来你太懒了,不能自己运行代码…抱歉,但我无能为力。好的@felix kling,我会澄清我的问题,抱歉误解
if($posted_quantity <= $current_quantity){
if($posted_quantity <= $min_allowed_quantity){
//do something
}
elseif($posted_quantity >= $max_allowed_quantity){
//do something else
}
}