I have a question about how PHP handles assignment of variable values.
we have the following statement:
$myVariable = "value";
The above statement assigns a value to a variable $myVariableand then allocates a memory cell to it.
But if we add the following statement to the above script:
$secondVariable = $myVariable;
Then:
As for performance problems, it is recommended to avoid duplication of values and continue to use links if the value is not changed (C ++, Dietel and Dietel, the famous book "How to program in C ++")
But what about PHP? I heard, just heard, that PHP does some tricks and manages such duplications ( $secondVariable = $myVarible) by calling by reference rather than by value, and does not duplicate the variable until some changes happen with $ secondVariable, and after that will be made duplication.
Conclusion:
$myVariable = "value";
$secondVariable = $myVariable;
Something like this in C ++:
string myVariable = "value";
string secondVariable = &myVariable;
Although I know that PHP is written in C, C ++ is a close descendant of C.
Can someone say if the above is correct, and if PHP manages such variables or doesn’t care, and how C&C++does it create a new memory cell each time a value is assigned?
source
share