PHP:测试三个变量是否相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4702852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Testing whether three variables are equal
提问by Matty
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_below_average) { }
回答by Marc B
回答by PeeHaa
$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.
将是另一种方式来做到这一点。
回答by Dogbert
if ($select_above_average === $select_average
&& $select_average === $select_below_average) { }
回答by RobertPitt
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.
您已经有了 Adam 的答案,但是记住如何正确执行此操作的一个好方法是记住进行一次验证时您应该将其包裹在()
大括号中,如果您只进行一次检查,那么您已经拥有 if ( )声明。
Example:
例子:
if (a === b)
如果( 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:
如果你记得每组大括号都是一个验证检查,你可以像这样登录:
if( ((a === b)|| (c === d))&& (e === f))
if( (( 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.
if 语句和许多其他逻辑操作适用于层次结构,因此检查中的单个检查数量会影响父检查。
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.
以上面的第三个例子 if a === b
or c === d
failed thene === f
永远不会被检查,因为 ab,cd 被包裹在大括号中,以便返回和检查。
Hope this helps you a little more.
希望这对你有更多帮助。
回答by Joseph Astrahan
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 以了解更多它的作用。