PHP references and pointers [duplicate] - php

This question already has answers here:
Are there pointers in php?
(9 answers)
Closed 6 years ago.
Is there any difference between pointers and references?
When should I use references?
What actually this syntax does?
$a = &$b

Do not use references! They can lead to unexpected behavoiur.
As at PHP.net: One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.
There are pointers and references, you can see it it example below.
$a = new stdClass ($a now is a pointer to #168)
$b = $a ($b is now a new pointer, but it still points to #168)
$c = &$a ($c is now a reference to pointer $a)
you can't distinguish $b from $c
$a = null, it's just not a pointer anymore, just null
#168 is kept, because there are pointers to it left
$b is still a pointer to #168
$c is now null
This behavior is dangerous, because it alters variable you don't touch.
$b === $c // returns true
$youCantSayThisVariableIsRelated = null
$b === $c // returns false!

Related

Why can I infere that TRUE == FALSE in php? [duplicate]

This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
A friend of mine recently showed my the following snippet
<?php
$a = 0;
$b = 'x';
if(FALSE == $a && $a == $b && $b == TRUE) {
echo 'WTF!?';
}
?>
which ouputs WTF!?
I understand why FALSE == $a holds, because zero is considered to be FALSE. I also understand why $b == TRUE holds, because the string is not empty. But I didn't understand why $a == $b is true, could somebody explain to me what type coercion rules play together here to get this rather amusing result?
when you compare $a == $b you are comparing an int with a string so PHP tries to parse the string into an int if it fails which happened in this case, it changes its value to 0 and then 0 == 0 returns true. Check this:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Change this condition $a == $b to $a === $b to compare the type as well. Hope this helps.

Testing reference equality on a recursive array

<?php
$a = [];
$b = [&$a];
$a[] = &$b;
Assuming we have a recursive array - is there any PHP builtin that will test for equality including references?
Standard comparison operators throw a fatal error, and while array_diff doesn't crash it does spit out a ton of notices.
I could always compare print_r($a, true) == print_r($b, true) but this would incorrectly identify $a and $b as the same (Their values may be the same but the references they contain aren't)

How to use a pointer in a PHP function? [duplicate]

This question already has answers here:
Are there pointers in php?
(9 answers)
Closed 7 years ago.
In C we can use pointer for a function parameter:
test( *text);
function test( *txt){
//codes goes here
}
Is it possible in PHP?
Variable names in PHP start with $ so $entryId is the name of a variable.
$this is a special variable in Object Oriented programming in PHP, which is reference to current object.
-> is used to access an object member (like properties or methods) in PHP, like the syntax in C++.
So your code means this: place the value of variable $entryId into the entryId field (or property) of this object.
The & operator in PHP, means pass reference. Here is an example:
$b=2;
$a=$b;
$a=3;
print $a;
print $b;
// output is 32
$b=2;
$a=&$b; // note the & operator
$a=3;
print $a;
print $b;
// output is 33
In the above code, because we used & operator, a reference to where $b is pointing is stored in $a. So $a is actually a reference to $b.
There is a good explanation of pointers on this page
There are references, but that's not the same as pointers.
php.net has multiple pages explaining What References Are, What References Do and What References Are Not.
There too, it is mentioned multiple times that
References are not pointers.
They provide a way to assign $a to $b so that if you reassign $b, $a changes as well:
$a = 'a';
$b = &$a; // Reference
$b = 'b'; // Now $a == 'b'
This can be used for function arguments as well:
function myFunc(&$b)
{
$b = 'b';
}
$a = 'a';
myFunc($a); // Now $a == 'b'
Before PHP 5.3.0 is was also possible to do a thing called "call-time pass-by-reference", where you would have a "normal" function declaration and use the & operator in the call instead:
function myFunc($b)
{
$b = 'b';
}
$a = 'a';
myFunc(&$a); // As of PHP 5.3.0 produces a Fatal Error, saying:
// Call-time pass-by-reference has been removed; If you would like to pass argument by reference, modify the declaration of myFunc().
But beware! Assigning another reference will not update the original reference:
$a = 'a';
$b = 'b';
$c = &$a;
$c = &$b; // $a == 'a'
[ Demo ]
A trap resulting from that exists with the global keyword.
$a = 'a';
$b = 'b';
function myFunc()
{
global $a, $b;
$a = &$b;
var_dump($a);
}
myFunc(); // 'b'
var_dump($a); // 'a'
That is because global $a effectively means $a = &$GLOBALS['a'], so assigning it a new reference will not update $GLOBALS.
Of course you can prevent that by using $GLOBALS directly.
But if you're using globals at all, you should probably rethink your design anyway.
With references, there is now also a difference between setting a variable = NULL and using unset().
= NULL follows references, unset() does not:
$a = 'a';
$b = &$a;
unset($b); // $a == 'a'
$a = 'a';
$b = &$a;
$b = NULL; // $a == NULL
[ Demo ]
Bottom line:
References allow for things that wouldn't be possible without them, but sometimes do not behave the way one would expect them to, because of how PHP was built.

PHP operator difference && and "and" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
'AND' vs '&&' as operator
Sorry for very basic question but I started learning PHP just a week ago & couldn't find an answer to this question on google/stackoverflow.
I went through below program:
$one = true;
$two = null;
$a = isset($one) && isset($two);
$b = isset($one) and isset($two);
echo $a.'<br>';
echo $b;
Its output is:
false
true
I read &&/and are same. How is the result different for both of them? Can someone tell the real reason please?
The reason is operator precedence. Among three operators you used &&, and & =, precedence order is
&&
=
and
So $a in your program calculated as expected but for $b, statement $b = isset($one) was calculated first, giving unexpected result. It can be fixed as follow.
$b = (isset($one) and isset($two));
Thats how the grouping of operator takes place
$one = true;
$two = null;
$a = (isset($one) && isset($two));
($b = isset($one)) and isset($two);
echo $a.'<br>';
echo $b;
Thats why its returning false for first and true for second.
Please see:
http://www.php.net/manual/en/language.operators.logical.php
It explains that "and" is different from "&&" in that the order of operations is different. Assignment happens first in that case. So if you were to do:
$b = (isset($one) and isset($two));
You'd end up with the expected result.

Fallthrough variable assignment in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Coalesce function for PHP?
I'm not sure what this is normally called, but I hope the title communicates well enough. What I have is a handful of variables some of which might be null.
I want to do:
$a = $b || $c || $d;
Where $a ends up being = to the first non-null variable.
To my knowledge, PHP doesn't support this in the same way JavaScript does.
You can, however do something like this:
$a = $b ? $b : ($c ? $c : $d);
A more general solution:
function fallthrough($arr) {
//$arr should be an array of possible values. The first non-null value is returned
do $a = array_shift($arr);
while($a === null && $arr);
return $a;
}
<?php
$a = 0;
$b = false;
$c = true; //should become this
$d = '1';
$e = $a ?: $b ?: $c ?: $d;
var_dump($e);
//bool(true)
//should be '1' if order is different
$e = $a ?: $b ?: $d ?: $c;
var_dump($e);
//string(1) "1"
... however ?: is kinda new, you will confuse your colleagues / fellow coders.
I don't think that's possible. I think you'd have to use some other, more laborious, way. I.e. make an array of the variables, iterate through it until you find a non-null value and break the loop, like so:
$vars = array("b" => $b, "c" => $c, "d" => $d);
foreach($vars as $var) {
if($var != null) {
$a = $var;
break;
}
}
Well, like some other answers here say, you can use the shorthand way of writing this, but writing readable code is important too. The above code is pretty readable.

Categories