<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>J &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/j/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>It&#8217;s Not True that PHP Arrays are Copied by Value</title>
		<link>/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/</link>
		<comments>/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/#comments</comments>
		<pubDate>Fri, 17 Aug 2012 14:08:32 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Array slicing]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C programming language]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[J]]></category>

		<guid isPermaLink="false">/?p=3288</guid>
		<description><![CDATA[PHP, Arrays &#038; Passing by Reference Do you know that objects in PHP5 are passed by reference, while arrays and other scalar variables are passed by value? Yes, you know it, but it&#8217;s not exactly true. Let&#8217;s see some example and let&#8217;s try to answer few questions. // depending on the machine but both lines &#8230; <a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" class="more-link">Continue reading <span class="screen-reader-text">It&#8217;s Not True that PHP Arrays are Copied by Value</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>PHP, Arrays &#038; Passing by Reference</h2>
<p>Do you know that objects in PHP5 are passed by reference, while arrays and other scalar variables are passed by value? Yes, you know it, but it&#8217;s not exactly true. Let&#8217;s see some example and let&#8217;s try to answer few questions.</p>
<pre lang="PHP">
// depending on the machine but both lines return
// expectedly equal values: 331208

// 331208
echo memory_get_usage();
echo memory_get_usage();
</pre>
<p>These two lines of code, expectedly return the same value (in my case 331208), which shows us that because nothing happened in between them the memory usage isn&#8217;t growing. Let&#8217;s now put some code in between them.</p>
<pre lang="PHP">
echo memory_get_usage(); // 331616
$a = 10;
echo memory_get_usage(); // 331696
</pre>
<p><span id="more-3288"></span></p>
<p>Now because of the variable $a, we get a little more memory consuption! The same thing (with even more memory usage) happens if we have an array initialization.</p>
<pre lang="PHP">
echo memory_get_usage(); // 332128
$a = array(1, 2, 3, 'hello', 'world');
echo memory_get_usage(); // 332728
</pre>
<p>OK, now we see how much memory PHP is using for this very simple and small array. If we copy this array, we&#8217;d expect PHP to take twice as much memory, but that&#8217;s not the case!</p>
<pre lang="PHP">
echo memory_get_usage(); // 332336
$a = array(1, 2, 3, 'hello', 'world');
$b = $a;
echo memory_get_usage(); // 332984
</pre>
<p>Actually if we had $b = 10; this will consume more!!! memory than the code above.</p>
<pre lang="PHP">
echo memory_get_usage(); // 332336
$a = array(1, 2, 3, 'hello', 'world');
$b = 10;
echo memory_get_usage(); // 333016
</pre>
<p>This is simply because in the first case <strong>$b wasn&#8217;t a copy</strong> of $a, while in the second case we have a brand new variable on the ground, which, of course, requires memory.</p>
<h2>Why Arrays aren&#8217;t Copied?</h2>
<p>Actually now we see that copying by reference and by value is absolutely the same.</p>
<pre lang="PHP">
$a = array(1, 2, 3, 'hello', 'world');

// this line is exactly the same as ...
$b = $a;

// this line
$b = &$a;
</pre>
<p>That is because in both case the array is passed by reference. It is copied once we make changes to $b. Then in the first case $b becomes a copy of $a and it&#8217;s changed, while in the second case $b is exactly the same array as $a and every change to $b changes $a as well.</p>
<pre lang="PHP">
echo memory_get_usage();
$a = array(1, 2, 3, 'hello', 'world');
echo memory_get_usage();
$b = $a;
$b = array(1, 2, 3, 'goodby', 'world');
echo memory_get_usage();
</pre>
<h2>Conclusion</h2>
<p>If we take an example from Zend Framework, where often we work with arrays that are passed to the view as a &#8220;copy&#8221;:</p>
<pre lang="PHP">
$a = array(1, 2, 3, 'hello', 'world');
$this->view->a = $a; // this is NOT a copy
</pre>
<p>This will not consume more memory!!! The only way to make your application more memory inefficient is to change directly the $this->view->a array, so be careful!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/10/19/thing-to-know-about-php-arrays/" rel="bookmark" title="Thing to Know About PHP Arrays">Thing to Know About PHP Arrays </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/05/19/php-associative-arrays-coding-style/" rel="bookmark" title="PHP Associative Arrays Coding Style">PHP Associative Arrays Coding Style </a></li>
<li><a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" rel="bookmark" title="Object Cloning and Passing by Reference in PHP">Object Cloning and Passing by Reference in PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
