Because $a=’a’ allocates one byte but $b is not modified.
In PHP, for the code “$a=’a’;$b=$a;” allocates 2 units of memory or 1 unit?
how can I test it?
I can say that pseudoly like
alphahandler = new stringObj
alphahandler->insertContent('a');
variable a = alphahandler.objectHandler();
comes to b
variable b = a.getCurrentObjectHandler()
or just like
b=a.getCurrentObject().toContent()
??
How many times is memory allocated?
Ignoring that both variables also take up memory (and require separate variable entries), there is only one string object created from the above code.
That is, both variables now refer to the same value: there was no “copy” of the string created upon assignment.
How much memory is allocated?
There is much more memory being allocated (than say, 1 or 2 bytes): there is the memory for the object and then there is the memory for each variable.
While this question relates to integers, similar stuff happens for strings (actually, more happens). A simple integer takes at least 72 bytes of memory. As mario suggested in a comment, this example may require upwards of 400 bytes.
Happy coding.