International PHP Conference 2019 - Spring Edition

Voting

Please answer this simple SPAM challenge: min(five, three)?
(Example: nine)

The Note You're Voting On

dallgoot
4 months ago
After some headaches, here is a function to check if, between 2 variables $a,$b check if one is a reference to the other.
It means they "point" to the same value.
Tested on :
PHP 7.2.2 (cli) (built: Jan 31 2018 19:31:17) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Hope that helps...
<?php

function are_references(&$a, &$b){
   
$mem = $a;     //memorize
   
$a = uniqid ("REFERENCE???", true ); //change $a
   
$same = $a === $b; //compare
   
$a = $mem; //restore $a
   
return $same;
}
echo
"***distinct vars AND distinct values\n";
$a = "toto";
$b = "tata";

var_dump($a, $b, are_references($a, $b));
echo
"verify original values: $a, $b\n";

echo
"***distinct vars BUT SAME values\n";
$a = "toto";
$b = "toto";

var_dump($a, $b, are_references($a, $b));
echo
"verify original values: $a, $b\n";

echo
'*** $b is a reference of $a'."\n";
$a = "titi";
$b = &$a;

var_dump($a, $b, are_references($a, $b));
echo
"verify original values: $a, $b\n";

echo
'*** $a is a reference of $b'."\n";
$b = "titi";
$a = &$b;

var_dump($a, $b, are_references($a, $b));
echo
"verify original values: $a, $b\n";
?>
Result:
***distinct vars AND distinct values
string(4) "toto"
string(4) "tata"
bool(false)
verify original values: toto, tata
***distinct vars BUT SAME values
string(4) "toto"
string(4) "toto"
bool(false)
verify original values: toto, toto
*** $b is a reference of $a
string(4) "titi"
string(4) "titi"
bool(true)
verify original values: titi, titi
*** $a is a reference of $b
string(4) "titi"
string(4) "titi"
bool(true)
verify original values: titi, titi

<< Back to user notes page

To Top