I've never come across this before, but how would you test whether three variables are the same? The following, obviously doesn't work but I can't think of an elegant (and correct) way to write the following:
我以前从来没有遇到过这个问题,但是您如何测试三个变量是否相同呢?下面,显然行不通,但我想不出一种优雅(和正确)的方式来写以下内容:
if ($select_above_average === $select_average === $select_below_average) { }
if ($select_above_average === $select_average === $select_average === = $select_below_average) {}
5 个解决方案
#2
21
$values = array($select_above_average, $select_average, $select_below_average);
if(count(array_unique($values)) === 1) {
// do stuff if all elements are the same
}
Would be another way to do it.
这是另一种方法。
#3
8
if ($select_above_average === $select_average
&& $select_average === $select_below_average) { }
#4
5
you already have your answer by Adam but a good way to remember how to do this correctly is to remember for a single validation you should be wrapping in ()
braces, if your only doing one single check then you already have the braces provided by the if ( ) statement.
你已经有你的答案,亚当,但记得如何正确做到这一点的一个好方法就是记住一个验证你应该包装在()括号,如果你只做一个检查,那么你已经提供的括号()语句。
Example:
例子:
if ( a === b )
if (a === b)
and if your doing multiple then
如果你做的是倍数
if( ( a === b ) && ( c === d ) )
if(a == b) & (c == d)
Sop if you remember that every set of braces is a validation check, you can have login like this:
Sop如果你记得每组括号都是验证检查,你可以这样登录:
if( (( a === b ) || ( c === d )) && ( e === f ) )
(a == b) || (c == d) && (e === f)
if statements and many other logical operations work on hierarchy so that the amount of individual checks within a check has an effect on he parent check.
如果语句和许多其他逻辑操作在层次结构上起作用,那么一个检查中的单个检查的数量会对父检查产生影响。
taking the third example above if a === b
or c === d
fails then e === f
will never be checked as the ab,cd is wrapped in braces so that is returned and checked.
如果a == b或c == d失败,则使用上面的第三个例子,那么e == f将永远不会被检查为ab,cd被包裹在大括号中,因此返回并检查。
Hope this helps you a little more.
希望这能对你有所帮助。
#5
1
I had a unique situation in which I needed to see if the amount of items in three arrays was the same much like this scenario.
我有一个独特的情况,我需要查看三个数组中的项目数量是否与这个场景相同。
This is what I came up with:
这就是我想到的:
(Assume that fields, operators and values are all arrays)
(假设字段、操作符和值都是数组)
$allfieldscount = array(count($fields), count($operators), count($values)); //store an array of the count of all the arrays.
$same = array_count_values($allfieldscount);//returns an array by values in the array. We are looking to see only 1 item in the array with a value of 3.
if(count($same) != 1){
//Then it's not the same
}else{
//Then it's the same
}
This tactic counts the fields in the different arrays and by using array_count_values if they are all the same then the count of the array it returns will be '1', if it's anything else then it's not the same. Look up array_count_values on php.net to understand more what its doing.
这种方法计算不同数组中的字段,如果它们都是相同的,则使用array_count_values,那么它返回的数组的计数将是“1”,如果它是其他的,则不是相同的。在php.net上查找array_count_values,了解它的操作。
#1
#2
21
$values = array($select_above_average, $select_average, $select_below_average);
if(count(array_unique($values)) === 1) {
// do stuff if all elements are the same
}
Would be another way to do it.
这是另一种方法。
#3
8
if ($select_above_average === $select_average
&& $select_average === $select_below_average) { }
#4
5
you already have your answer by Adam but a good way to remember how to do this correctly is to remember for a single validation you should be wrapping in ()
braces, if your only doing one single check then you already have the braces provided by the if ( ) statement.
你已经有你的答案,亚当,但记得如何正确做到这一点的一个好方法就是记住一个验证你应该包装在()括号,如果你只做一个检查,那么你已经提供的括号()语句。
Example:
例子:
if ( a === b )
if (a === b)
and if your doing multiple then
如果你做的是倍数
if( ( a === b ) && ( c === d ) )
if(a == b) & (c == d)
Sop if you remember that every set of braces is a validation check, you can have login like this:
Sop如果你记得每组括号都是验证检查,你可以这样登录:
if( (( a === b ) || ( c === d )) && ( e === f ) )
(a == b) || (c == d) && (e === f)
if statements and many other logical operations work on hierarchy so that the amount of individual checks within a check has an effect on he parent check.
如果语句和许多其他逻辑操作在层次结构上起作用,那么一个检查中的单个检查的数量会对父检查产生影响。
taking the third example above if a === b
or c === d
fails then e === f
will never be checked as the ab,cd is wrapped in braces so that is returned and checked.
如果a == b或c == d失败,则使用上面的第三个例子,那么e == f将永远不会被检查为ab,cd被包裹在大括号中,因此返回并检查。
Hope this helps you a little more.
希望这能对你有所帮助。
#5
1
I had a unique situation in which I needed to see if the amount of items in three arrays was the same much like this scenario.
我有一个独特的情况,我需要查看三个数组中的项目数量是否与这个场景相同。
This is what I came up with:
这就是我想到的:
(Assume that fields, operators and values are all arrays)
(假设字段、操作符和值都是数组)
$allfieldscount = array(count($fields), count($operators), count($values)); //store an array of the count of all the arrays.
$same = array_count_values($allfieldscount);//returns an array by values in the array. We are looking to see only 1 item in the array with a value of 3.
if(count($same) != 1){
//Then it's not the same
}else{
//Then it's the same
}
This tactic counts the fields in the different arrays and by using array_count_values if they are all the same then the count of the array it returns will be '1', if it's anything else then it's not the same. Look up array_count_values on php.net to understand more what its doing.
这种方法计算不同数组中的字段,如果它们都是相同的,则使用array_count_values,那么它返回的数组的计数将是“1”,如果它是其他的,则不是相同的。在php.net上查找array_count_values,了解它的操作。