<?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>Comparison of programming languages &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/comparison-of-programming-languages/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>
		<item>
		<title>PHP: Arrays or Linked Lists?</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/</link>
		<comments>/2012/07/24/php-arrays-or-linked-lists/#comments</comments>
		<pubDate>Tue, 24 Jul 2012 11:25:20 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[data structures]]></category>
		<category><![CDATA[$_head]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Extinction]]></category>
		<category><![CDATA[Foreach]]></category>
		<category><![CDATA[Linked list]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Stack]]></category>
		<category><![CDATA[UnShuffle sort]]></category>

		<guid isPermaLink="false">/?p=3259</guid>
		<description><![CDATA[Arrays vs. Linked List If we talk about arrays and linked lists we know the pros and cons about both of them. No matter which programming language we use arrays benefit from direct access to its items, while linked lists are more memory efficient for particular tasks. The items of a linked list keep a &#8230; <a href="/2012/07/24/php-arrays-or-linked-lists/" class="more-link">Continue reading <span class="screen-reader-text">PHP: Arrays or Linked Lists?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/06/14/computer-algorithms-linked-list-data-structure/" rel="bookmark" title="Computer Algorithms: Linked List">Computer Algorithms: Linked List </a></li>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Arrays vs. Linked List</h2>
<p>If we talk about arrays and linked lists we know the pros and cons about both of them. No matter which programming language we use arrays benefit from direct access to its items, while linked lists are more memory efficient for particular tasks.</p>
<figure id="attachment_3279" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/07/Array-Linked-List.png"><img src="/wp-content/uploads/2012/07/Array-Linked-List.png" alt="Array &amp; Linked List" title="Array &amp; Linked List" width="620" height="314" class="size-full wp-image-3279" srcset="/wp-content/uploads/2012/07/Array-Linked-List.png 620w, /wp-content/uploads/2012/07/Array-Linked-List-300x151.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Array &#038; Linked List</figcaption></figure>
<p>The items of a linked list keep a reference to their successor, so we can easily walk through the entire list. However we don&#8217;t have direct access to its elements. Thus we can&#8217;t go directly to its middle element! Even more &#8211; in particular implementations of a linked list we don&#8217;t know its length. But in some cases linked lists are far more effective than arrays. For instance reversing an array of non-numeric values require constant additional memory, but also requires n/2 exchanges. The same taks using linked lists is not only performed in linear time, but doesn&#8217;t require any additional memory. The only thing we need to do is to reverse the links &#8211; no movement of values and the items remain at the same place in the memory. </p>
<p>Merging of two arrays often require more space (proportional of the space of the two arrays) or many exchanges in case we try to do it in place. The same task on linked lists is far more effective with only changing pointers and without moving the values.<span id="more-3259"></span></p>
<h2>Arrays or Linked Lists are More Memory Efficient</h2>
<p>Many developers consider linked lists as something used only in college, but actually they can be very useful in practice as well. However how practically useful they are? Let&#8217;s see the following PHP experiment.</p>
<p>Here we have one class called &#8220;Item&#8221;, which is designed to keep only one integer value as its key and to point to its successor. Practically this class is designed to be used by a singly linked list, but let say we put some of these objects into an array and the same amount of the &#8220;Item&#8221; objects into a linked lists so what are the results?</p>
<p>First let&#8217;s see the code!</p>
<pre lang="PHP">
class Item
{
    protected $_key = '';
    protected $_next = null;
    
    public function __construct($key)
    {
        $this->_key = $key;
    }
    
    public function setNext(&$next) { $this->_next = $next; }
    public function &getNext() { return $this->_next; }
    
    public function setKey($key) { $this->_key = $key; }
    public function getKey() { return $this->_key; }
    
    public function __toString()
    {
        return $this->_key . "\n";
    }
}
</pre>
<p>This is the &#8220;Item&#8221; class and here we have the Linked_List class. As you can see this is the very basic implementation of a linked list with only one &#8220;insert&#8221; method and the magic __toString() in order to print the entire list. The insert method pushes an item at the end of the list thus the insertion is O(1).</p>
<pre lang="PHP">
class Linked_List 
{
    protected $_head = null;
    protected $_tail = null;
    
    public function insert($item)
    {
        if ($this->_head == null) {
            $this->_head = $item;
            $this->_tail = $item;
            return;
        }
        
        $this->_tail->setNext($item);
        $this->_tail = $item;
    }
    
    public function __toString()
    {
        $current = $this->_head;
        $output = '';
        
        while ($current) {
            $output .= $current->getKey() . "\n";
            $current = $current->getNext();
        }
        
        return $output;
    }
}
</pre>
<p>Now let&#8217;s see the creation of an array with N objects of class &#8220;Item&#8221;.</p>
<pre lang="PHP">
$n = 10000;
$a = array();
for ($i = 0; $i < $n; $i++) {
    $a[$i] = new Item($i);
}
</pre>
<p>The same thing but using the Linked_List class follows on the lines below.</p>
<pre lang="PHP">
$n = 10000;
$a = new Linked_List();
for ($i = 0; $i < $n; $i++) {
    $a->insert(new Item($i));
}
</pre>
<h2>And the Winner is ...</h2>
<p>More memory efficient is ... the linked list! On the next chart we can see the results. It's clear that for 10K objects the array uses nearly 1MB more memory than the linked list! </p>
<figure id="attachment_3280" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/07/Array-vs.-Linked-List-Chart.png"><img src="/wp-content/uploads/2012/07/Array-vs.-Linked-List-Chart.png" alt="Array vs. Linked List Chart" title="Array vs. Linked List Chart" width="600" height="371" class="size-full wp-image-3280" srcset="/wp-content/uploads/2012/07/Array-vs.-Linked-List-Chart.png 600w, /wp-content/uploads/2012/07/Array-vs.-Linked-List-Chart-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>So what do you think now? Will you use linked list in your code or not?</p>
<h2>Final Words</h2>
<p>Although the linked list seems to be more memory efficient we don't have direct acess to it's items. In the same time often we don't need direct access, we just need to walk through the array, which doesn't benefit from the direct access. In PHP this is usally done with some loop construction as "foreach". So why we have such results in the experiment above. First our linked list is really very basic. It doesn't have any functionality, which in fact shouldn't affect memory usage much more. The array in the other hand keeps indexes for each of its items so this results in additional space. This explains a bit the victory of the linked list in the memory efficiency test.</p>
<p>In the other hand PHP can't have the full benefit of using linked lists, trees and other data structures since it keeps them in memory only for the request. In this case C, C++, Java loads a data structure in memory till the software runs so unfortunately coding complex data structures in PHP doesn't look as a great option. Indeed here we have an entire "Item" class only to keep an integer. Instead we can use an array of integers! </p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/06/14/computer-algorithms-linked-list-data-structure/" rel="bookmark" title="Computer Algorithms: Linked List">Computer Algorithms: Linked List </a></li>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/07/24/php-arrays-or-linked-lists/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Object Cloning and Passing by Reference in PHP</title>
		<link>/2011/10/27/object-cloning-and-passing-by-reference-in-php/</link>
		<comments>/2011/10/27/object-cloning-and-passing-by-reference-in-php/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 14:25:17 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clone]]></category>
		<category><![CDATA[Cloning]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Java programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">/?p=2408</guid>
		<description><![CDATA[In PHP everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples. Passing Parameters by Reference Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this. function f($param) { $param++; } $a = 5; &#8230; <a href="/2011/10/27/object-cloning-and-passing-by-reference-in-php/" class="more-link">Continue reading <span class="screen-reader-text">Object Cloning and Passing by Reference in PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>In <a href="/category/php/" title="PHP at stoimen.com">PHP </a>everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples.<br />
<figure id="attachment_2432" style="width: 480px" class="wp-caption alignnone"><a href="/wp-content/uploads/2011/10/ampersand.jpg"><img src="/wp-content/uploads/2011/10/ampersand.jpg" alt="Passing by reference in PHP can be tricky!" title="ampersand" width="480" height="480" class="size-full wp-image-2432" srcset="/wp-content/uploads/2011/10/ampersand.jpg 480w, /wp-content/uploads/2011/10/ampersand-150x150.jpg 150w, /wp-content/uploads/2011/10/ampersand-300x300.jpg 300w" sizes="(max-width: 480px) 100vw, 480px" /></a><figcaption class="wp-caption-text">Some developers think that everything&#039;s passed by reference in PHP.</figcaption></figure></p>
<h2>Passing Parameters by Reference</h2>
<p>Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this.</p>
<pre lang="PHP">
function f($param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the value of $a equals 5. If it were passed by reference, it would be 6. With a little change of the code we can get it.</p>
<pre lang="PHP">
function f(&$param)
{
	$param++;
}

$a = 5;
f($a);

echo $a;
</pre>
<p>Now the variable&#8217;s value is 6. </p>
<p>So far, so good. Now what about copying objects?<br />
<span id="more-2408"></span></p>
<h2>Objects: A Copy or a Cloning?</h2>
<p>We can check whether by assigning an object to a variable a reference or a copy of the object is passed.</p>
<pre lang="PHP">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = $a;

$b->myvar = 20;

// 20, not 10
echo $a->myvar;
</pre>
<p>The last line outputs 20! This makes it clear. By assigning an object to a variable PHP pass its reference. To make a copy there&#8217;s another approach. We need to change $b = $a, to $b = clone $a;</p>
<pre lang="PHP" escaped="true">
class C
{
	public $myvar = 10;
}

$a = new C();
$b = clone $a;

$b->myvar = 20;

// 10
echo $a->myvar;
</pre>
<h2>Arrays by Reference</h2>
<p>What about arrays? What if I assign an array to a variable?</p>
<pre lang="PHP">
$a = array(20);

$b = $a;
$b[0] = 30;

var_dump($a);
</pre>
<p>What do you think is the value of $a[0]? Well, the answer is: 20! So $b is a copy of the array &#8220;a&#8221;. Instead you should assign explicitly its reference to make &#8220;b&#8221; point to &#8220;a&#8221;.</p>
<pre lang="PHP">
$a = array(20);

$b = &$a;
$b[0] = 30;

var_dump($a);
</pre>
<p>Now $a[0] equals 30!</p>
<p>I think this could be useful!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/08/17/its-not-true-that-php-arrays-are-copied-by-value/" rel="bookmark" title="It&#8217;s Not True that PHP Arrays are Copied by Value">It&#8217;s Not True that PHP Arrays are Copied by Value </a></li>
<li><a href="/2012/04/26/php-strings-dont-need-quotes/" rel="bookmark" title="PHP Strings Don&#8217;t Need Quotes">PHP Strings Don&#8217;t Need Quotes </a></li>
<li><a href="/2011/10/20/some-notes-on-the-object-oriented-model-of-php/" rel="bookmark" title="Some Notes on the Object-oriented Model of PHP">Some Notes on the Object-oriented Model of PHP </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/27/object-cloning-and-passing-by-reference-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Thing to Know About PHP Arrays</title>
		<link>/2011/10/19/thing-to-know-about-php-arrays/</link>
		<comments>/2011/10/19/thing-to-know-about-php-arrays/#respond</comments>
		<pubDate>Wed, 19 Oct 2011 15:18:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[C programming language]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Foreach]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[PHP arrays]]></category>
		<category><![CDATA[PHP micro tutorial]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">/?p=2390</guid>
		<description><![CDATA[Consider the following case. We have an array with identical keys. $arr = array(1 => 10, 1 => 11); What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, &#8230; <a href="/2011/10/19/thing-to-know-about-php-arrays/" class="more-link">Continue reading <span class="screen-reader-text">Thing to Know About PHP Arrays</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </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="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Consider the following case. We have an array with identical keys. </p>
<pre lang="PHP">
$arr = array(1 => 10, 1 => 11);
</pre>
<p>What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, where those identical keys are represented once as an integer and then as a string.<br />
<figure id="attachment_2404" style="width: 640px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/10/php.code_.jpg"><img src="/wp-content/uploads/2011/10/php.code_.jpg" alt="Keys in PHP arrays are not type sensitive, so pay attention when using them!" title="PHP Code" width="640" height="480" class="size-full wp-image-2404" srcset="/wp-content/uploads/2011/10/php.code_.jpg 640w, /wp-content/uploads/2011/10/php.code_-300x225.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /></a><figcaption class="wp-caption-text">Keys in PHP arrays are not type sensitive, so pay attention when using them!</figcaption></figure></p>
<pre lang="PHP">
$arr = array(1 => 10, "1" => 11);
</pre>
<p>Now several questions arise. First of all, how many elements have this array? Two or one. This can be easily verified by checking what count() will return.<span id="more-2390"></span></p>
<pre lang="PHP">
echo count($arr);
</pre>
<p>The correct answer is 1. This simply means, that there&#8217;s no difference between string keys and integer keys. What would happen if we had a &#8220;normal&#8221; array with different keys?</p>
<pre lang="PHP">
$arr = array(1 => 10, "2" => 11);
echo count($arr);
</pre>
<p>As expected this returns 2. </p>
<p>Next thing to check is what&#8217;s in the array after this initialization line.</p>
<pre lang="PHP">
$arr = array(1 => 10, "1" => 11);
</pre>
<p>Is there something in the first element $arr[0], or there&#8217;s something in the second element $arr[1]? What is the value of the single value?<br />
As it appears the second element replaces the first one. We&#8217;ve seen that the array has only one value, but where&#8217;s that value? The only way to check this is to dump both elements:</p>
<pre lang="PHP">
var_dump($arr);
</pre>
<p>Here we can see that $arr[1] contains &#8220;11&#8221; and it is the only value, and $arr[0] is not set.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" rel="bookmark" title="PHP: What is More Powerful Than list() &#8211; Perhaps extract()">PHP: What is More Powerful Than list() &#8211; Perhaps extract() </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="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/10/19/thing-to-know-about-php-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: What is More Powerful Than list() &#8211; Perhaps extract()</title>
		<link>/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/</link>
		<comments>/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 15:49:35 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[Associative array]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[post]]></category>

		<guid isPermaLink="false">/?p=1942</guid>
		<description><![CDATA[list() in PHP Recently I wrote about list() in PHP which is indeed very powerful when assigning variable values from array elements. $a = array(10, array('here', 'are', 'some', 'tests')); list($count, $list) = $a; Actually my example in the post was not correct, because I wrote that you can pass an associative array, but the truth &#8230; <a href="/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/" class="more-link">Continue reading <span class="screen-reader-text">PHP: What is More Powerful Than list() &#8211; Perhaps extract()</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
<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="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>list() in PHP</h2>
<p>Recently I wrote <a title="PHP: what is more powerful than list" href="/2010/08/27/php-what-is-more-powerful-than-list/" target="_blank">about list()</a> in PHP which is indeed very powerful when assigning variable values from array elements.</p>
<pre lang="php">
$a = array(10, array('here', 'are', 'some', 'tests'));
list($count, $list) = $a;
</pre>
<p>Actually my example in the post was not correct, because I wrote that you can pass an associative array, but the truth is that you cannot, and thus the array should be always with numeric keys. After noticing the comments of that post, and thanks to @Philip,  I searched a bit about how this problem can be overcome.</p>
<h2>There is a Solution</h2>
<p>As always PHP gives a perfect solution! You can see on the <a title="PHP: list - Manual" href="http://php.net/manual/en/function.list.php" target="_blank">list() doc page</a> that there is a function that may help you use an associative array.</p>
<h2>Extract</h2>
<p>extract() is perhaps less known than list(), but it does the right thing!</p>
<pre lang="php">
$a = array('count' => 10, 'list' => array('here', 'are', 'some', 'tests'));
extract($a);

echo $count;    // 10
print_r($list); // array('here'....
</pre>
<p>Note that now both $count and $list are defined.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
<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="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
