<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Programming Dive</title>
    <description>The latest articles on Forem by Programming Dive (@programmingdive).</description>
    <link>https://forem.com/programmingdive</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F408383%2F618ccd7e-d536-4061-95d8-3b24aacd148d.png</url>
      <title>Forem: Programming Dive</title>
      <link>https://forem.com/programmingdive</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/programmingdive"/>
    <language>en</language>
    <item>
      <title>Convert an Array To StdClass With PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Thu, 19 Nov 2020 08:08:38 +0000</pubDate>
      <link>https://forem.com/programmingdive/convert-an-array-to-stdclass-with-php-2adc</link>
      <guid>https://forem.com/programmingdive/convert-an-array-to-stdclass-with-php-2adc</guid>
      <description>&lt;p&gt;For more details, &lt;a href="https://programmingdive.com/convert-an-array-to-stdclass-with-php/"&gt;click on this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To Convert an Array to &lt;a href="https://www.php.net/manual/en/language.types.object.php"&gt;stdClass&lt;/a&gt; with PHP is an easy step. In this tutorial, we will use different ways using which we can easily achieve the desired outcome.&lt;/p&gt;

&lt;p&gt;Before jumping right into the solution, let's understand a few things about stdClass is in &lt;a href="https://programmingdive.com/category/php/"&gt;PHP&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;What is stdClass in PHP?&lt;/h3&gt;

&lt;blockquote&gt;
stdClass stands for standard Class
&lt;/blockquote&gt;

&lt;p&gt;stdClass is just a generic empty class in PHP OR we can say it’s PHP’s class prototype like Object.prototype in JavaScript, Object in Java OR object in Python.&lt;/p&gt;

&lt;p&gt;Here, point to be noted that despite generic class, stdClass is NOT the base class for objects in PHP and we can prove this using instanceof keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;nature&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nv"&gt;$objNature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;nature&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$objNature&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="n"&gt;stdClass&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Yes'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'No'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

No
&lt;/pre&gt;

&lt;p&gt;This clarifies that stdClass is not the base class for objects in PHP.&lt;/p&gt;

&lt;p&gt;Now, we will see different ways useful to convert an array to stdClass.&lt;/p&gt;

&lt;p&gt;3 different ways to convert array to stdClass.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using Typecast&lt;/li&gt;
&lt;li&gt;Using custom function&lt;/li&gt;
&lt;li&gt;Using json_encode() and json_decode()&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Solution 1: Using typecast/ Type Juggling&lt;/h4&gt;

&lt;p&gt;Typecasting an array to a stdClass object is the easiest way to achieve. It will convert value of one data type into another data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$empInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'Houston'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$empInfoObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$empInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$empInfoObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

stdClass Object 
(
   [name] =&amp;gt; John 
   [address] =&amp;gt; Houston 
)
&lt;/pre&gt;

&lt;p&gt;Here, we defined an array &lt;code&gt;$empInfo&lt;/code&gt; in which we defined 3 keys and associated values and in the next line, we typecast our array into an object using the name of the desired type, written in parentheses before the variable which is to be cast.&lt;/p&gt;

&lt;p&gt;We can now access the property of the object with object-oriented style syntax as &lt;code&gt;$empInfoObj-&amp;gt;name&lt;/code&gt; which will output John.&lt;/p&gt;

&lt;p&gt;This is the simplest solution to our problem. But what if the array is multidimensional? Does the above solution work? And the answer is NO.&lt;/p&gt;

&lt;p&gt;Let’s update our $empInfo array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$empInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'Houston'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'employment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Los Angeles'&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$empInfoObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$empInfo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$empInfoObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

stdClass Object 
( 
[name] =&amp;gt; John 
[address] =&amp;gt; Houston 
[employment] =&amp;gt; Array 
     ( 
         [id] =&amp;gt; 1 
         [address] =&amp;gt; Los Angeles 
     ) 
)
&lt;/pre&gt;

&lt;p&gt;As we can see, an array defined inside the employment key does not get converted to a stdClass. So, type juggling casts the values at the opt level keys and not the keys which are defined in a nested pattern.&lt;/p&gt;

&lt;p&gt;So, here we can write our customize function to solve the problem.&lt;/p&gt;

&lt;h4&gt;Solution 2: Using custom function&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Return object &lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;array_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'toObject'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$empInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'Houston'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'employment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt; 
        &lt;span class="p"&gt;(&lt;/span&gt;
           &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Los Angeles'&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$empInfo&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

stdClass Object 
( 
   [name] =&amp;gt; John 
   [address] =&amp;gt; Houston 
   [employment] =&amp;gt; stdClass Object 
      ( 
         [id] =&amp;gt; 1 
         [address] =&amp;gt; Los Angeles 
      ) 
)
&lt;/pre&gt;

&lt;p&gt;Here, in the above example, we have recursively called &lt;code&gt;toObject()&lt;/code&gt; function to check the innermost array present (if any) and then typecast it into an object.&lt;/p&gt;

&lt;h4&gt;Solution 3: Using json_encode() and json_decode()&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;json_encode()&lt;/code&gt; and &lt;code&gt;json_decode()&lt;/code&gt; are the functions especially for performing operations on JSON string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;json_encode()&lt;/code&gt;json_encode()&amp;lt;/code is used to convert a JSON string into an Array.&lt;/p&gt;

&lt;p&gt;So, first, we’ll convert an object into a JSON string and then will convert it into an object using &lt;code&gt;json_decode()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$empInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s1"&gt;'address'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'Houston'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="s1"&gt;'employment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Los Angeles'&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;json_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$empInfo&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

stdClass Object
(
   [name] =&amp;gt; John
   [address] =&amp;gt; Houston
   [employment] =&amp;gt; stdClass Object
      (
         [id] =&amp;gt; 1
         [address] =&amp;gt; Los Angeles
      )
)&lt;/pre&gt;

&lt;p&gt;Instead of writing a custom function, we can use these in-built functions to get the desired output and also we don’t need to worry about the innermost array element as these two functions will take care of them.&lt;/p&gt;

&lt;h3&gt;Conclusion:&lt;/h3&gt;

&lt;p&gt;These 3 ways of Converting an Array To StdClass With PHP gives better options depending upon the requirement. Mostly in today's web frameworks we commonly deal with class hierarchy. So converting an array into stdClass at runtime will be a better option.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Convert an Array To String with PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Thu, 19 Nov 2020 07:50:55 +0000</pubDate>
      <link>https://forem.com/programmingdive/convert-an-array-to-string-with-php-500g</link>
      <guid>https://forem.com/programmingdive/convert-an-array-to-string-with-php-500g</guid>
      <description>&lt;p&gt;For more details, &lt;a href="https://programmingdive.com/convert-an-array-to-string-with-php/"&gt;check details here&lt;/a&gt;&lt;br&gt;&lt;br&gt;
To convert an Array to String with PHP, we can use two different built-in functions available in PHP. These two functions only take one array at a time to convert it into a string.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using json_encode() function&lt;/li&gt;
&lt;li&gt;Using implode() function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Using json_encode() function to convert an Array to a string&lt;/h3&gt;

&lt;p&gt;To convert an array to a string, one of the common ways to do that is to use the json_encode() function which is used to returns the JSON representation of a value.&lt;/p&gt;

&lt;p&gt;The syntax for using this function remains very basic-&lt;/p&gt;

&lt;blockquote&gt;
json_encode ( mixed$value [, int $options = 0 [, int $depth = 512 ]] ) : string|false
&lt;/blockquote&gt;

&lt;p&gt;This function takes any value as input except resource. But in our case, we’ll use an Array as an input value, and then this function will convert it into a JSON representation of the provided value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'BMW'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Germany'&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'Ferrari'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Italy'&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'Honda'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Japan'&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cars&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]
&lt;/pre&gt;

&lt;p&gt;At first glance, it doesn’t look like a string but this how JSON looks like. If we use var_dump() over json_encode() then it will show its data type as a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;var_dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cars&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

string(57) "[{"BMW":"Germany"},{"Ferrari":"Italy"},{"Honda":"Japan"}]"
&lt;/pre&gt;

&lt;h3&gt;Using implode function to convert an array to a string&lt;/h3&gt;

&lt;p&gt;The syntax for implode function is quite simple-&lt;/p&gt;

&lt;blockquote&gt;
implode ( string $glue , array $pieces ) : string
&lt;/blockquote&gt;

&lt;p&gt;Here,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$glue:&lt;/code&gt; is the string / special character(s) used to concatenate array values. Default is an empty string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$pieces:&lt;/code&gt; is the array whose values will stick together using glue.&lt;/p&gt;

&lt;p&gt;This will return all the Array elements concatenated together using glue in the same sequential order in which they appear in the array.&lt;/p&gt;

&lt;h4&gt;1. Using Indexed Array&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="c1"&gt;// using indexed array&lt;/span&gt;
&lt;span class="nv"&gt;$cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'BMW'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Ferrari'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Honda'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$cars_together&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;implode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;", "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$cars&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$cars_together&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

BMW, Ferrari, Honda
&lt;/pre&gt;

&lt;h4&gt;2. Using Associative array&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$cars&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'BMW'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Germany'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Ferrari'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Italy'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Honda'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Japan'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$cars_together&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;implode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;", "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$cars&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$cars_together&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Germany, Italy, Japan
&lt;/pre&gt;

&lt;p&gt;As mentioned above, all the values from the array will be stick together. Hence, if there is a situation where we need to get the values of the associative array to be glued together then use the same function.&lt;/p&gt;

&lt;h4&gt;3. Using Multidimensional Array&lt;/h4&gt;

&lt;p&gt;A multidimensional array can be simple OR complicated depending upon the situation in the project requirement. We will see the basic multidimensional array and for that, we need to write a callback function that will concatenate the values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
$automobile = array(
  array(
    'BMW' =&amp;gt; 'Germany'
  ),
  array(
    'Ferrari' =&amp;gt; 'Italy'
  ),
  array(
    'Honda' =&amp;gt; 'Japan'
  )
);
echo implode(', ', array_map(function ($entry) {
  return ($entry[key($entry)]);
}, $automobile));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Germany, Italy, Japan
&lt;/pre&gt;

&lt;h3&gt;Conclusion:&lt;/h3&gt;

&lt;p&gt;We can use any of the methods to fulfill the requirement. But when need to decide either to use json_encode() OR serialize(), I would always suggest using json_encode() because it takes smaller storage space as compared to serialize.&lt;/p&gt;

</description>
      <category>php</category>
      <category>array</category>
      <category>string</category>
    </item>
    <item>
      <title>How to merge an array in PHP?</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sun, 01 Nov 2020 14:52:25 +0000</pubDate>
      <link>https://forem.com/programmingdive/how-to-merge-an-array-in-php-4f2e</link>
      <guid>https://forem.com/programmingdive/how-to-merge-an-array-in-php-4f2e</guid>
      <description>&lt;p&gt;Please check the detailed post about &lt;a href="https://programmingdive.com/how-to-merge-an-array-in-php/"&gt;How to merge an array in PHP&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To merge arrays, PHP has provided inbuilt functions that will take arrays and will provide final output as a merged array. In this tutorial, we’ll see how to merge an array in PHP.&lt;/p&gt;

&lt;p&gt;PHP has provided different functions to group different arrays into single.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;array_merge()&lt;/li&gt;
&lt;li&gt;array_combine()&lt;/li&gt;
&lt;li&gt;array_merge_recursive()&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Merge two arrays using array_merge()&lt;/h3&gt;

&lt;p&gt;We can merge two or more arrays using array_merge() and its syntax is quite simple-&lt;/p&gt;

&lt;blockquote&gt;array_merge ([ array $... ] ) : array&lt;/blockquote&gt;

&lt;p&gt;Here, we can pass as many arrays (as arguments) as possible and the expected output will always be an array.&lt;/p&gt;

&lt;p&gt;So, in &lt;code&gt;array_merge()&lt;/code&gt; the resultant array will contain one array appended to another array which means the first array argument will be considered as a parent/reference array to which all the other array elements will be appended.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'apple'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'banana'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$flowers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'rose'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'lotus'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$final_array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$fruits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$flowers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array ( 
   [0] =&amp;gt; apple 
   [1] =&amp;gt; banana 
   [2] =&amp;gt; rose 
   [3] =&amp;gt; lotus 
)
&lt;/pre&gt;

&lt;p&gt;Now, consider a situation wherein the second array, the same string key is present. So, in such cases, the latter will override the first value.&lt;/p&gt;

&lt;p&gt;In the below example, let’s see how to merge associative array in php.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'fruit'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'color'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'orange'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'color'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$array2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array ( 
    [fruit] =&amp;gt;
    [color] =&amp;gt; red 
)
&lt;/pre&gt;

&lt;p&gt;However, if the arrays contain numeric keys, the later value will not overwrite the previous/original value, but it will be appended and a new index will be allocated to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Jack"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Kevin"&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Sparrow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Jason"&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$arr2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array ( 
   [0] =&amp;gt; 1 
   [1] =&amp;gt; Jack
   [name] =&amp;gt; Jason 
   [2] =&amp;gt; 3 
   [3] =&amp;gt; Sparrow 
   [4] =&amp;gt; 9 
)
&lt;/pre&gt;

&lt;p&gt;So, we can see that we have two same numeric and string keys in &lt;code&gt;$arr1&lt;/code&gt; &amp;amp; &lt;code&gt;$arr2&lt;/code&gt;. But in the case of numeric keys, it gets appended and for strings keys, it gets overrides.&lt;/p&gt;

&lt;p&gt;Now, if the provided array does not have numeric/string keys and only has values like &lt;code&gt;['NY', 'AL', 'AR']&lt;/code&gt; then those will be indexed in increment order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Jack"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Kevin"&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$states&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'NY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'AL'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'AR'&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$states&lt;/span&gt; &lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array ( 
   [0] =&amp;gt; 1 
   [1] =&amp;gt; Jack 
   [name] =&amp;gt; Kevin 
   [2] =&amp;gt; NY 
   [3] =&amp;gt; AL 
   [4] =&amp;gt; AR 
)
&lt;/pre&gt;

&lt;h3&gt;Merge multidimensional array in php&lt;/h3&gt;

&lt;p&gt;Merging a multidimensional array is the same as that of a simple array. It takes two arrays as input and merges them.&lt;/p&gt;

&lt;p&gt;In a multidimensional array, the index will be assigned in increment order and will be appended after the parent array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'kevin'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'US'&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Jason'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'US'&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$arr2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array
(
   [0] =&amp;gt; Array
       (
           [name] =&amp;gt; kevin
           [address] =&amp;gt; US
       )
   [1] =&amp;gt; Array 
       ( 
           [name] =&amp;gt; Jason 
           [address] =&amp;gt; US 
       )
)
&lt;/pre&gt;

&lt;p&gt;Now, let’s take the example of an employee who has two addresses. One is his/her permanent address and another is work address. Now, we fetched both the addresses and try to merge them in a single array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$permanent_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'employee'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'LA'&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$employment_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'employee'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'AL'&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$permanent_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$employment_location&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array ( 
     [employee] =&amp;gt; Array 
            ( 
               [address] =&amp;gt; AL
            ) 
)
&lt;/pre&gt;

&lt;p&gt;Well, it looks weird right. We wanted to get both the address merged into a single array. But this is not going to happen because as we can see both the array keys are the same and are string keys. So the quickest solution will be to store both the address in different indexes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$permanent_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'employee_home'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'LA'&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$employment_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'employee_current'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'AL'&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$permanent_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$employment_location&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array
(
   [employee_home] =&amp;gt; Array
       (
           [address] =&amp;gt; LA
       )
   [employee_current] =&amp;gt; Array 
       ( 
           [address] =&amp;gt; AL 
       )
)
&lt;/pre&gt;

&lt;p&gt;Otherwise user another useful PHP function to merge array recursively i.e, &lt;code&gt;array_merge_recursive()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Merge two arrays using array_merge_recursive()&lt;/h3&gt;

&lt;p&gt;Now, we’ll use the same above example and see how &lt;code&gt;array_merge_recursive()&lt;/code&gt; work efficiently to not override the parent key instead it uses the same key and add those different values inside it as an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_merge_recursive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$permanent_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$employment_location&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array
(
    [employee] =&amp;gt; Array
       (
           [address] =&amp;gt; Array
              (
                  [0] =&amp;gt; LA
                  [1] =&amp;gt; AL
              )
       )
)
&lt;/pre&gt;

&lt;p&gt;Now, suppose we have an array value for address ‘LA’ inside our first array &lt;code&gt;$permanent_location&lt;/code&gt; as follows then that value will also get recursively merged to the same key (address)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$permanent_location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'employee'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Albert"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;222&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;pre&gt;
// OUTPUT

Array
(
    [employee] =&amp;gt; Array
      (
         [address] =&amp;gt; Array
            (
                [name] =&amp;gt; Albert
                [2] =&amp;gt; 222
                [3] =&amp;gt; 999
            )
      )
)
&lt;/pre&gt;

&lt;h3&gt;Combine arrays using array_combine()&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;array_combine()&lt;/code&gt; is used to creates an array by using one array for keys and another for its values.&lt;/p&gt;

&lt;p&gt;So, we can use this function in different situations like user profile page where the user’s id, name, and address can be stored in one array and users’ actual information can be stored in another array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$table_headings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sr_no'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'address'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$row1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Jason'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Houston'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;array_combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$table_headings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$row1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>php</category>
      <category>array</category>
      <category>merge</category>
    </item>
    <item>
      <title>Which one to use PHPExcel or PHPSpreadsheet?</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sun, 18 Oct 2020 17:49:50 +0000</pubDate>
      <link>https://forem.com/programmingdive/which-one-to-use-phpexcel-or-phpspreadsheet-c6a</link>
      <guid>https://forem.com/programmingdive/which-one-to-use-phpexcel-or-phpspreadsheet-c6a</guid>
      <description>&lt;p&gt;For more detailed post visit &lt;a href="https://programmingdive.com/which-one-to-use-phpexcel-or-phpspreadsheet/"&gt;Which one to use PHPExcel or PHPSpreadsheet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Excel is an excellent tool to organize the data in the form of cells, perform basic and complex mathematical operations, create helpful graphics and charts, and many more. In this tutorial, we’ll see which one to use PHPExcel or PHPSpreadsheet.&lt;/p&gt;

&lt;p&gt;Using PHP, we can perform many useful operations and for this purpose, there are 2 famous libraries available. PHPExcel &amp;amp; PHPSpreadsheet.&lt;/p&gt;

&lt;p&gt;The main difference between these two is that PHPSpreadsheet is the upgraded version of PHPExcel. PHPSpreadsheet contains the latest features introduced in the newer version of the PHP i.e., v5.5.&lt;/p&gt;

&lt;h3&gt;How to use PHPEXcel?&lt;/h3&gt;

&lt;p&gt;To install PHPExcel in the current project, first, we need to copy Classes folder to your desired location. This location can be the project root classes folder and secondly, we need to copy the PHPExcel.php file that is the entry point for the Excel operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/** Include PHPExcel */&lt;/span&gt;
&lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="nb"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/Classes/PHPExcel.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PHPExcel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// write data to first sheet&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setActiveSheetIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// write data to defined excel sheet&lt;/span&gt;
&lt;span class="nv"&gt;$objWriter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PHPExcel_IOFactory&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;createWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Excel2007'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$objWriter&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Classes/studentInformation.xlsx'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s understand above example line by line-&lt;/p&gt;

&lt;p&gt;First, we included PHPExcel class file which will perform all the excel related operations. This can either be included using include/require statement OR it is recommended to use autoload to make sure that once the application boots, then this class should be available automatically.&lt;/p&gt;

&lt;p&gt;After creating PHPExcel object, we use method chaining to perform multiple operations on the active sheets one by one. Here, &lt;code&gt;setActiveSheetIndex()&lt;/code&gt; is used to set the active sheet to be used in the Workbook. In our case, we used index 0 to work on the first sheet.&lt;/p&gt;

&lt;p&gt;Then, we added values in the A1, A2, B1, B2. In real-world example this could be multiple rows by using &lt;code&gt;for()&lt;/code&gt; / &lt;code&gt;foreach()&lt;/code&gt; loop. To set each cell values, we used &lt;code&gt;setCellValue()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Once everything is set, now our data is ready to be written in the excel. for this, we used &lt;code&gt;PHPExcel_IOFactory&lt;/code&gt; class which is used to perform write operation using &lt;code&gt;createWriter()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;PHPExcel_IOFactory&lt;/code&gt; class can write data in different formats like-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Excel2007&lt;/li&gt;
&lt;li&gt;Excel5&lt;/li&gt;
&lt;li&gt;Excel2003XML&lt;/li&gt;
&lt;li&gt;OOCalc&lt;/li&gt;
&lt;li&gt;SYLK&lt;/li&gt;
&lt;li&gt;Gnumeric&lt;/li&gt;
&lt;li&gt;HTML&lt;/li&gt;
&lt;li&gt;CSV&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, this will save excel sheet to the mentioned location in the &lt;code&gt;save()&lt;/code&gt; function&lt;/p&gt;

&lt;h3&gt;How to write data in multiple sheets using PHPExcel?&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/** Include PHPExcel */&lt;/span&gt;
&lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="nb"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/Classes/PHPExcel.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PHPExcel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// write data to first sheet&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setActiveSheetIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// add new sheet to the same excel &lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createSheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// write data to second sheet in the same workbook&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setActiveSheetIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alexa'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// set focus to first sheet of the workbook&lt;/span&gt;
&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setActiveSheetIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// write data to defined excel sheet&lt;/span&gt;
&lt;span class="nv"&gt;$objWriter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PHPExcel_IOFactory&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;createWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$objPHPExcel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Excel2007'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$objWriter&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Classes/studentInformation.xlsx'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, in the code, we have added a new function right after updating sheet 0 which is &lt;code&gt;$objPHPExcel-&amp;gt;createSheet();&lt;/code&gt; which adds a new sheet in the existing workbook.&lt;/p&gt;

&lt;p&gt;After that, we did the same operation as that of the sheet 0 by providing index 1 to the &lt;code&gt;setActiveSheetIndex()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Many times, we need to set focus on the first sheet rather than the last one for the obvious reason. So that can be done via calling the first sheet again i.e., &lt;code&gt;$objPHPExcel-&amp;gt;setActiveSheetIndex(0);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, we learned how to create a new workbook/excel and add different sheets and write data into it.&lt;/p&gt;

&lt;p&gt;Now, let’s see about PHPSpreadsheet.&lt;/p&gt;

&lt;h3&gt;What is PHPSpreadsheet?&lt;/h3&gt;

&lt;p&gt;PhpSpreadsheet is a library written in PHP that offers read and writes various spreadsheet file formats such as Excel and LibreOffice Calc and many more. Basically, it is an advanced version of PHPExcel with more format supports and support to the new PHP version.&lt;/p&gt;

&lt;p&gt;PhpSpreadsheet requires a minimum PHP 5.6 version to support its functionalities.&lt;/p&gt;

&lt;p&gt;Let’s see the basic example of using PHPSpreadsheet and then we’ll see it step by step-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpOffice\PhpSpreadsheet\Spreadsheet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpOffice\PhpSpreadsheet\Writer\Xlsx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$spreadsheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Spreadsheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$sheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$spreadsheet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getActiveSheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$sheet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Xlsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$spreadsheet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$writer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'studentInformation.xlsx'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, in the above example, we have autoloaded all the necessary library files needed for excel operations.&lt;/p&gt;

&lt;p&gt;Next, we imported our spreadsheet class necessary to perform an operation on excel just like we did for the Excel in PHPExcel functionality and also included the writer to write the data in the spreadsheet.&lt;/p&gt;

&lt;p&gt;After creating a spreadsheet object, we then use &lt;code&gt;getActiveSheet()&lt;/code&gt; function to get the active sheet. Here, we can see that, we do not pass the index parameter which means the spreadsheet object is smart enough to pick the currently active sheet. Then, we updated the spreadsheet with values using &lt;code&gt;setCellValue()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Finally, we passed all the spreadsheet object to the writer class (&lt;code&gt;new Xlsx($spreadsheet)&lt;/code&gt;) object to write data in the excel and save using &lt;code&gt;studentInformation.xlsx&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Now, what if we want to write multiple sheets at a time. So, in this case, we’ll use the same object and add another sheet-like below.&lt;/p&gt;

&lt;h3&gt;How to write data in multiple sheets using PHPSpreadsheet?&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpOffice\PhpSpreadsheet\Spreadsheet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpOffice\PhpSpreadsheet\Writer\Xlsx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$spreadsheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Spreadsheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$sheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$spreadsheet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getActiveSheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$sheet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$sheet_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$spreadsheet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createSheet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$sheet_2&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setCellValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alexa'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$writer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Xlsx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$spreadsheet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$writer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'studentInformation.xlsx'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used &lt;code&gt;createSheet()&lt;/code&gt; function which we also used in the PHPExcel library example which eventually will create a new sheet in the same workbook.&lt;/p&gt;

&lt;p&gt;Now, with all this information, the main question still remains the same.&lt;/p&gt;

&lt;h3&gt;Conclusion:&lt;/h3&gt;

&lt;p&gt;PHPExcel library is now archived because of the old PHP version functionalities. It is always recommended to use the latest PHP version to enjoy the new features in it.&lt;/p&gt;

&lt;p&gt;There are so many additional functions available in both the libraries which can be used based on the requirement.&lt;/p&gt;

&lt;p&gt;So to answer the main question, which one to use PHPExcel or PHPSpreadsheet depends on the current version of the PHP installed on the server and the requirement. If PHPExcel is incapable of providing certain features that PHPSpreadsheet can provide then it is always good to upgrade the PHP version but with that, we also need to check the deprecated functions and need to work on them&lt;/p&gt;

</description>
      <category>php</category>
    </item>
    <item>
      <title>Autocomplete using PHP and Ajax</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sat, 03 Oct 2020 06:38:27 +0000</pubDate>
      <link>https://forem.com/programmingdive/autocomplete-using-php-and-ajax-5c27</link>
      <guid>https://forem.com/programmingdive/autocomplete-using-php-and-ajax-5c27</guid>
      <description>&lt;p&gt;&lt;b&gt;Original &amp;amp; detailed post is written at &lt;a href="https://programmingdive.com/autocomplete-using-php-and-ajax/"&gt;Autocomplete using PHP and Ajax&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In the previous tutorials, we learned about &lt;a href="https://programmingdive.com/how-to-upload-image-using-ajax-and-javascript/"&gt;uploading an image using AJAX and Javascript&lt;/a&gt;. So in this tutorial, we’ll see autocomplete using PHP and Ajax.&lt;/p&gt;

&lt;p&gt;Autocomplete plays an important role in web development and with the help of ajax we can let users get an excellent experience. So let’s first understand what autocomplete is?&lt;/p&gt;

&lt;p&gt;When a user types a particular character(s) to search in the text field and the system gives a list of matching contents without refreshing the page then that process is called autocomplete.&lt;/p&gt;

&lt;p&gt;Steps to be followed (in this tutorial) for autocomplete using PHP and Ajax-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create HTML form&lt;/li&gt;
&lt;li&gt;Use JQuery for Ajax to get a list of matching names&lt;/li&gt;
&lt;li&gt;Get the list of matching names using cURL and return to the user in the form of a list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Creating HTML Form&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Demo Example&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Astronauts:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"astronauts"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"astronauts"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"astronauts-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ve used only one form field for the demo. In practical examples, there might be many more fields. Here, we have defined one text field for astronauts and this will suggest their names once we start tying.&lt;/p&gt;

&lt;h4&gt;Use JQuery for Ajax to get a list of matching names&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;ready&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#astronauts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;keyup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;response.php&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;dataType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="na"&gt;astronaut_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#astronauts-list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;show&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;lists&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nx"&gt;lists&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;li onclick='highlightSelectedAstronauts(&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;)'&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
                &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#astronauts-list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lists&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;700&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;highlightSelectedAstronauts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#astronauts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#astronauts-list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the complete javascript code. Let’s understand it block by block.&lt;/p&gt;

&lt;p&gt;So, when document loads, we initialize keyup event for our field “astronauts”. It means whenever user types then this keyup event gets triggered and it performs ajax operation.&lt;/p&gt;

&lt;p&gt;In the ajax call, once we received response from PHP then we show all the matching astronauts in the form of list in “li” and on clicking any name that astronaut will be selected in the text field.&lt;/p&gt;

&lt;p&gt;Here, we have also delayed the request (using delay function) to the server by few milliseconds because sometime user types the name very fast and ajax requests the server multiple time which takes time to fetch the actual result.&lt;/p&gt;

&lt;p&gt;If the matching list is huge then it will eventually takes longer time than the short list. So, the solution is to delay the request to the server by few milliseconds which not only lowers the burden on the server but also respond very quickly..&lt;/p&gt;

&lt;h4&gt;Get the list of matching names using cURL&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$astronaut_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_POST&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'astronaut_name'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nv"&gt;$cURLConnection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;curl_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'http://api.open-notify.org/astros.json'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;curl_setopt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cURLConnection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CURLOPT_RETURNTRANSFER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$apiResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;curl_exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cURLConnection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;curl_close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cURLConnection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nv"&gt;$list_astros&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$apiResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list_astros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'success'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list_astros&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'people'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$astronaut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;preg_match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$astronaut_name&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$astronaut&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt; &lt;span class="nv"&gt;$output_array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$output_array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;array_push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$astronaut&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code we used cURL to get the list of astronauts. We can also fetch the list from database. But for the demo I think we can keep cURL for shorter code.&lt;/p&gt;

&lt;p&gt;In the cURL request, we fetch the current astronauts which are currently present in the ISS (International Space Station). Once we get that list then we can start with collecting only that list which is currently matching with what user has requested.&lt;/p&gt;

&lt;p&gt;In the next piece of code, we loop through each astronauts fetched using cURL and whatever the user has requested will be matched with each astronauts one by one and if it matches then that will be collected in the separate array and then using json_encode() function we’ll return json string to ajax call.&lt;/p&gt;

&lt;h3&gt;Conclusion:&lt;/h3&gt;

&lt;p&gt;Giving user best experience when it comes to larger website is the good approach but that doesn’t mean the website should be fancy. User must not be irritated when he/she requires particular list and he/she has to keep waiting.&lt;/p&gt;

&lt;p&gt;Autocomplete using PHP and ajax saves not only users time and but it also increases usability as well.&lt;/p&gt;

</description>
      <category>php</category>
      <category>jquery</category>
      <category>ajax</category>
    </item>
    <item>
      <title>Understanding of self and this in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Tue, 11 Aug 2020 16:35:53 +0000</pubDate>
      <link>https://forem.com/programmingdive/understanding-of-self-and-this-in-php-6oc</link>
      <guid>https://forem.com/programmingdive/understanding-of-self-and-this-in-php-6oc</guid>
      <description>&lt;p&gt;In this tutorial, we’ll start understanding of self and this in PHP &amp;amp; will see when to use &lt;code&gt;self&lt;/code&gt; and &lt;code&gt;$this&lt;/code&gt; in PHP while development &amp;amp; will understand how can we use &lt;code&gt;self&lt;/code&gt; &amp;amp; &lt;code&gt;$this&lt;/code&gt; in various situations.&lt;br&gt;
So, the basic question comes in mind, what is &lt;code&gt;self&lt;/code&gt;? and what is &lt;code&gt;$this&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;As we know that when we create any class then that class contains constants, variables aka “properties”, and functions aka “methods”.&lt;/p&gt;

&lt;p&gt;In OOP, to access these class components, we can use either $this OR self in PHP. It means, in PHP only we can use these ways to get control over different class components. This may differ based on the programming languages you use.&lt;/p&gt;

&lt;h3&gt;What is self:: in PHP?&lt;/h3&gt;

&lt;p&gt;A class may contain an amalgamation of static &amp;amp; non-static members. To access static members of the class, we use self along with scope resolution operator ::. This Scope Resolution Operator is also called Paamayim Nekudotayim (means double-colon in Hebrew) or in simpler words, a double colon. Static methods are also specified as namespaced global functions.&lt;/p&gt;

&lt;p&gt;Static functions and variables are associated with the class itself. It means whatever the value static variables hold, it will be shared among all the other created objects.&lt;/p&gt;

&lt;p&gt;In simple words, suppose we update static variable using $objBooks, and then using $newObjBook we again tried to access the same static variable then we’ll get the same value and vice-versa.&lt;/p&gt;

&lt;p&gt;Let’s see code in action-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nv"&gt;$bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;updateBookInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nv"&gt;$bookId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// object 1&lt;/span&gt;
&lt;span class="c1"&gt;// before update&lt;/span&gt;
&lt;span class="nv"&gt;$objBooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$objBooks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;updateBookInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$objBooks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// object 2&lt;/span&gt;
&lt;span class="nv"&gt;$newObjBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$newObjBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$newObjBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;updateBookInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// after update&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$objBooks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$newObjBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
// OUTPUT&lt;br&gt;

1&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
2
&lt;/blockquote&gt;

&lt;p&gt;Okay, so in this example, we have created a book class in which we have defined one static variable ($bookId) and to access it, we have defined getBookId() method. Now, first of all, we have created a book object and assign value to a static variable $bookId using updateBookInfo() method.&lt;/p&gt;

&lt;p&gt;This method uses self:: syntax to update a class variable $bookId. Once assigned, it can now be available for use inside and outside of the class.&lt;/p&gt;

&lt;p&gt;We can access this value using getBookId() method from inside or outside of the class OR directly using a class (book::$bookId) from outside of the class.&lt;/p&gt;

&lt;p&gt;Next, we have defined another object $newObjBook. Now, we will get the same value of $bookId using this object as well because of the property of the class-level variable.&lt;/p&gt;

&lt;p&gt;Once, we update the value of this static variable using updateBookInfo() e.g., 2 in our case, then that value becomes available to all the objects (including current, next, and previous objects).&lt;/p&gt;

&lt;p&gt;So, $objBooks-&amp;gt;getBookId(); &amp;amp; $newObjBooks-&amp;gt;getBookId(); will give the same value i.e., 2&lt;/p&gt;

&lt;h3&gt;How can we access static members of the class?&lt;/h3&gt;

&lt;p&gt;So, we have seen a basic understanding of how to define static variables, how we can access it, and how all instances of a class share the same static variable.&lt;/p&gt;

&lt;p&gt;There are different ways with which we can access static variable.&lt;br&gt;
So, static variables &amp;amp; functions are accessed via self::variableName &amp;amp; self::functionName() respectively (from inside of the class). In the example shown above, a static variable is accessed using self::$bookId when we are inside of the class.&lt;/p&gt;

&lt;p&gt;But from outside of the class, we need to use book::$bookId because static variables are class level variables. So there is no need to create a class object to access its static variables. We can directly access it via className then scope resolution operator ( :: ) followed by a variable name.&lt;/p&gt;

&lt;p&gt;Sometimes we also see parent:: keyword whose task is to access parent class methods. This syntax is usually found inside the method like __construct(){} where we include it at the very first line inside a function to inherit all the parent class constructor data. We can also use parent:: in other methods as well. But the truth is there is no such relation between self:: &amp;amp; parent::.&lt;/p&gt;

&lt;h3&gt;&lt;strong&gt;What is $this in PHP?&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;$this represents the reference to the current object of the class. $this is the pseudo-variable created as soon the object of the class is created and is available to use once that class method is called from within an object context.&lt;/p&gt;

&lt;p&gt;Let’s understand the $this using basic example-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setBookId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bookId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$objBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$objBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setBookId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$objBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
// OUTPUT&lt;br&gt;
&lt;br&gt;
123
&lt;/blockquote&gt;

&lt;p&gt;As we can see, we’ve defined two methods in class books and one is used to set bookId and another is used to get the bookId. For this, we used $this variable because we are inside of the class and we are dealing with class non-static variables from within the object context.&lt;/p&gt;

&lt;h3&gt;So, what does $this contains?&lt;/h3&gt;

&lt;p&gt;$this contains all the properties available within the class. There may be a misconception that when we print $this then it will give a list of all the members within the scope of the class.&lt;br&gt;
But this is NOT true. $this will print all the existing and new variables associated with the class itself.&lt;/p&gt;

&lt;p&gt;Yes, that’s true. We can also add a new variable(s) dynamically for temporary use to the class. You may be confused now because we’ve seen so many examples that contain the use of properties defined within a class.&lt;/p&gt;

&lt;p&gt;Let’s use the same example of book class and define dynamic variables defined outside of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setBookId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bookId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getBookId&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bookId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getCurrentObjInfo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;$objBook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;books&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$objBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setBookId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$objBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Jack"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$currObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$objBook&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getCurrentObjInfo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$currObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;blockquote&gt;
// OUTPUT&lt;br&gt;

books Object &lt;br&gt;
( &lt;br&gt;
   [bookId] =&amp;gt; 123 &lt;br&gt;
   [name] =&amp;gt; Jack &lt;br&gt;
)&lt;br&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’ve defined a new method getCurrentObjInfo() and inside it, we have printed the current object by passing $this parameter. At line number 22, we have defined a new variable and assigned it to object $objBook.&lt;/p&gt;

&lt;p&gt;So, when we called this new method getCurrentObjInfo() then it will not only print existing variables but also newly defined dynamic variables as well.&lt;/p&gt;

&lt;p&gt;Now, let’s discuss the main topic.&lt;/p&gt;

&lt;h3&gt;&lt;strong&gt;When to use self:: in PHP?&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;To know &lt;a href="https://programmingdive.com/understanding-of-self-and-this-in-php/#When_to_use_self_in_PHP"&gt;when to use self:: over $this&lt;/a&gt; check my post which I have written in depth.&lt;/p&gt;

&lt;p&gt;When to use $this in PHP?&lt;/p&gt;

&lt;p&gt;To know &lt;a href="https://programmingdive.com/understanding-of-self-and-this-in-php/#When_to_use_%24this_in_PHP"&gt;when to use $this in PHP&lt;/a&gt; check this link.&lt;/p&gt;

</description>
      <category>php</category>
    </item>
    <item>
      <title>header() function in PHP?</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Wed, 22 Jul 2020 05:21:14 +0000</pubDate>
      <link>https://forem.com/programmingdive/what-is-header-function-in-php-4ihk</link>
      <guid>https://forem.com/programmingdive/what-is-header-function-in-php-4ihk</guid>
      <description>&lt;p&gt;Original and detailed post of this tutorial is located at &lt;a href="https://programmingdive.com/what-is-header-function-in-php/"&gt;What is header() function in PHP?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As per php.net documentation, the header function is mainly used to send a raw HTTP header. Well, in the beginning, it is confusing to know the term raw HTTP. In this tutorial, we’ll see what is header() function in PHP, its importance, and its uses.&lt;/p&gt;

&lt;h2&gt;Use of header() function in PHP&lt;/h2&gt;

&lt;p&gt;header() function in PHP is used to send raw HTTP header and it must be called before any output is sent to the requester, either by normal HTML tags, blank lines in a file, or from PHP.&lt;/p&gt;

&lt;p&gt;The main purpose of header() function is to redirect user from the current/certain page to another URL.&lt;/p&gt;

&lt;p&gt;Let’s understand the syntax first and then we’ll see individual parameters.&lt;/p&gt;

&lt;blockquote&gt;header ( string $header [, bool $replace = TRUE [, int $http_response_code ]] ) : void&lt;/blockquote&gt;

&lt;p&gt;Where,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$header:&lt;/code&gt; is the header string which has two special-case header calls. One is the “Location:” and another is the “HTTP/“.&lt;br&gt;
&lt;code&gt;$replace:&lt;/code&gt; Optional. It indicates whether the header should replace a previously sent similar header, or add another header of the same type.&lt;br&gt;
&lt;code&gt;$http_response_code:&lt;/code&gt; It forces the HTTP response code to the specified value.&lt;/p&gt;

&lt;h3&gt;Using “Location:”&lt;/h3&gt;

&lt;p&gt;With “HTTP/” parameter can contain any HTTP status code to send. For example, if we have configured Apache to use a PHP script to handle requests for missing files then we can redirect user to the specified file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HTTP/1.0 404 Not Found"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show a 404 page not found page because we have added security restriction for accessing certain pages so that if the user tries to access those page then we’ll certainly show not found page.&lt;/p&gt;

&lt;h2&gt;Download file using header in PHP&lt;/h2&gt;

&lt;p&gt;The advantage of header function is that we can create and download files on the fly. This is a very important feature that is needed while working with files related operations in the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="c1"&gt;// what kind of document to be downloaded&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Content-Type: application/pdf'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// name the file as document.pdf&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Content-Disposition: attachment; filename="document.pdf"'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Read original.pdf file and output it&lt;/span&gt;
&lt;span class="nb"&gt;readfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'original.pdf'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are basic three steps to download any file at runtime.&lt;br&gt;
&lt;/p&gt;
&lt;ul&gt;

&lt;li&gt;Define type of file to be downloaded&lt;/li&gt;

&lt;li&gt;name that file&lt;/li&gt;

&lt;li&gt;read the content of the file/text&lt;/li&gt;

&lt;p&gt;Besides other intermediate steps, these are the 3 basic steps which are used to download file on the fly.&lt;/p&gt;

&lt;h2&gt;Set HTTP headers to prevent page caching&lt;/h2&gt;

&lt;p&gt;PHP is quite famous for its dynamic data. But there are certain situations where we don’t client browser to cached those data (to load website faster of course )&lt;/p&gt;

&lt;p&gt;We can use header() function to override such a situation and add certain parameters that will prevent webpages from being cached.&lt;br&gt;
&lt;/p&gt;

&lt;/ul&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"expires: Wed, 11 Jan 1984 05:00:00 GMT"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cache-control: no-cache, must-revalidate, max-age=0"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Here, we have added cache-control: no-cache to make sure that each time browser request for a particular webpage then server must send updated data rather than cached one.&lt;/p&gt;

&lt;p&gt;We’ve also added expires parameter to past date represents that web-page is already expired and new data is expected from the client browser.&lt;/p&gt;

&lt;h2&gt;Conclusion:&lt;/h2&gt;

&lt;p&gt;Introduced in PHP 4, header function plays an important role while development. May modern frameworks used this function in the core to redirect users to a specific function. Downloading file at runtime, setting cache-control is the of the main features in header() function&lt;/p&gt;

&lt;p&gt;I hope that I have covered all the related information about what is header() function in PHP and it’s related uses. If you have any questions Or you think any need for improvement then please comment.&lt;/p&gt;



</description>
      <category>php</category>
    </item>
    <item>
      <title>Return statement in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sat, 18 Jul 2020 17:50:01 +0000</pubDate>
      <link>https://forem.com/programmingdive/return-statement-in-php-3m1m</link>
      <guid>https://forem.com/programmingdive/return-statement-in-php-3m1m</guid>
      <description>&lt;p&gt;The return statement is an important language constraint to return the value to the caller. In this tutorial, we’ll see different uses of return statements in PHP with examples.&lt;/p&gt;

&lt;p&gt;I know that there is nothing special in creating this post only for return statement. But believe me, this will create new opportunities of using return statement in different part of the codebase and we’ll understand how dynamically we can use it in our day to day coding.&lt;/p&gt;

&lt;p&gt;In most of the scenarios, we have seen this return statement inside the function definition only.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingdive.com/different-uses-of-return-statement-in-php/"&gt;https://programmingdive.com/different-uses-of-return-statement-in-php/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
    </item>
    <item>
      <title>XML Parsing in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Thu, 16 Jul 2020 16:30:58 +0000</pubDate>
      <link>https://forem.com/programmingdive/xml-parsing-in-php-i4o</link>
      <guid>https://forem.com/programmingdive/xml-parsing-in-php-i4o</guid>
      <description>&lt;p&gt;XML stands for EXtensible Markup Language which is a markup language that is both human-readable and machine-readable.&lt;/p&gt;

&lt;p&gt;PHP has provided inbuilt function simplexml_load_string() to process XML and fetch information from it. This function was introduced in PHP in version 5.&lt;/p&gt;

&lt;p&gt;simplexml_load_string ( string $data [, string $class_name = “SimpleXMLElement”  [, int $options = 0 [, string $ns = “” [, bool $is_prefix = FALSE ]]]] ) : SimpleXMLElement&lt;/p&gt;

&lt;p&gt;Where-&lt;br&gt;
string $data : XML data in string format.&lt;br&gt;
string $class_name : optional parameter. It will return an object of the specified class (if any).&lt;br&gt;
int $options : Optional parameter. It specifies additional Libxml parameters.&lt;br&gt;
string $ns : Optional parameter. It specifies a namespace prefix or URI&lt;br&gt;
bool $is_prefix : Optional parameter. Returns TRUE if ns is a prefix, FALSE if ns is a URI. The default is FALSE.&lt;/p&gt;

&lt;p&gt;for more detail information check this link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingdive.com/understanding-xml-parsing-using-php/"&gt;https://programmingdive.com/understanding-xml-parsing-using-php/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>xml</category>
      <category>parsing</category>
    </item>
    <item>
      <title>Generators in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Thu, 18 Jun 2020 18:28:37 +0000</pubDate>
      <link>https://forem.com/programmingdive/using-generators-in-php-1i6p</link>
      <guid>https://forem.com/programmingdive/using-generators-in-php-1i6p</guid>
      <description>&lt;p&gt;PHP 5.5 came up with an interesting function to the PHP ecosystem and that was Generators. So while using generators in PHP, we’ll see what are generators and what are different ways of using it.&lt;/p&gt;

&lt;p&gt;The structure of generators are similar to the normal functions itself. But in generators instead of return statement, we use yield. Weird, right?. Basically this how generators need to be defined.&lt;/p&gt;

&lt;p&gt;So, generators basically loops through an data and avoids large amount of memory for that Array. Yes, generators works with an Array and while creating new array out of the loops it avoids utilizing large amount of memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingdive.com/using-generators-in-php/"&gt;https://programmingdive.com/using-generators-in-php/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Method Chaining in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sun, 14 Jun 2020 11:48:39 +0000</pubDate>
      <link>https://forem.com/programmingdive/method-chaining-in-php-4gmn</link>
      <guid>https://forem.com/programmingdive/method-chaining-in-php-4gmn</guid>
      <description>&lt;p&gt;In this tutorial, we’ll take a look at Method Chaining in PHP. Method Chaining is nothing but the concatenation of multiple methods to increase readability of code and avoid putting all the code in the single function.&lt;/p&gt;

&lt;p&gt;One of the major changes between PHP4 and PHP5 is that in PHP5 method can return objects. We will first see the basics of method chaining and then we’ll look into examples with explanation.&lt;/p&gt;

&lt;p&gt;Method Chaining is mainly used to implement Fluent Interface in an object-oriented API to achieve code readability. The term Fluent Interface was first invented by Eric Evans and Martin Fowler back in 2005. Note that method chaining is not Fluent Interface. Rather Fluent Interface uses method chaining.&lt;/p&gt;

&lt;p&gt;Using Fluent Interface, now we can allow multiple methods to be called on the same object using $this variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingdive.com/method-chaining-in-php/"&gt;https://programmingdive.com/method-chaining-in-php/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
    </item>
    <item>
      <title>Shorthand Comparisons in PHP</title>
      <dc:creator>Programming Dive</dc:creator>
      <pubDate>Sat, 13 Jun 2020 20:23:13 +0000</pubDate>
      <link>https://forem.com/programmingdive/shorthand-comparisons-in-php-g3o</link>
      <guid>https://forem.com/programmingdive/shorthand-comparisons-in-php-g3o</guid>
      <description>&lt;p&gt;In Programming, shorthand is writing same piece of code in shorter way. Shorthand comparisons in PHP comprise of minimal use of code for better reading &amp;amp; usability.&lt;/p&gt;

&lt;p&gt;In this tutorial, we’ll learn which shorthand syntax PHP has added and will understand usability of it with practical approach.&lt;/p&gt;

&lt;p&gt;First, we’ll understand the most basic and most important condition which we regularly use in day to day coding. The if statement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://programmingdive.com/shorthand-comparisons-in-php/"&gt;https://programmingdive.com/shorthand-comparisons-in-php/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
    </item>
  </channel>
</rss>
