Forum Moderators: coopster
<?
$a = "test";
$b = "test";
$c = "test";
$d = "test";
if ($a == $b &= $c &= $d){
echo "true";
}else{
echo "false";
}
?>
Does anyone use this operator? Can someone provide a link to the documentation on this maybe?
Thanks in advance for your help!
Its called 'Passing by Reference'. It means that both your variables point to the same content. This should help you out:
[uk.php.net...]
dc
$a = "test1";
$b = "test2";
$c = "test3";
$d = "test4";
if ($a == $b &= $c &= $d) {
echo "true";
}else{
echo "false";
}
echo '<br>';
echo "$a - $b - $c - $d";
Results in:
false
test1 - test0 - test0 - test4
Which is not what you require. (Bitwise Operators [php.net])
Just expanding on what enigma1 wrote above... The bitwise assignment operators work from right to left, so what your code is actually doing is equivalent to:
$c = $c & $d;
$b = $b & $c;
if ($a == $b) { ...
To check if 4 variables are the same you will need to do something like:
if ($a == ($b == ($c == $d))) {
echo "true";
} else {
echo "false";
}
TRUEas the value of variable $c which is "test" does indeed equal the value of variable $d which is "test". The return value of that expression is boolean
TRUE. Now you have ...
if ($a == ($b == ((bool)TRUE))) {
if ($a == ($b == ((bool) TRUE))) { Dang, of course - thanks!
In that case, it's just the usual...
if (($a == $b) && ($b == $c) && ($c == $d)) {
echo "true\n";
} else {
echo "false\n";
} IMHO, if you need to check for equality between many variables then I'd use an alternative memory structure eg. an array.
if (1 == sizeof(array_unique([$a, $b, $c, $d]))) {
// ...
}
Something like that.