48

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) { }

5 Answers 5

76
if ((a == b) && (b == c)) {
   ... they're all equal ...
}

by the transitive relation

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

5 Comments

Use ===, not ==. PHP's loose equality is not transitive!
It should be noted that the parentheses around each comparison are not required.
This can be continued like this (a === b && b === c && c === d && d === e) etc.. to check if multiple variables are all equal.
-1, == is not transitive in PHP, if you use values $a='0'; $b=0; $c='0_php_fail';, ($a == $b) && ($b == $c) evaluates to true, while $a == $c evaluates to false
I think saying they are entirely unrequired is rather an opinion than a fact. First off, the pharentheses make the statement much more readable, second off, removing them can lead to unexpected behaviour when using some language constructs. note that, for example, include uses everything after it as one single parameter while (include ...) ... would behave differently. sorry if this may be a bad example for you, but it's true for many php-language-"constructs". not only for include.
28
$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 Comments

Yowza, talk about driving 500 miles to get a Slurpee at the 7-11 instead walking down to the block to the 7-11 there.
:) That's why I said another way. Cause the answer wal already given :). I had to laugh though bout your comment.
This solution worked for my problem, which was how to check for a min/max number of equal values in a set (say, less than 3 out of 5 given values equal) with a single line. Thanks!
10
if ($select_above_average === $select_average
    && $select_average === $select_below_average) { }

Comments

6

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 )

and if your doing multiple then

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 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.

Hope this helps you a little more.

1 Comment

This was a useful and helpful example. Cheers!
3

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.

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.