PHP Array and String literal dereferencing Tutorial

Udemy Generic 728x90

Overview:

The topic “array and string literal dereferencing” itself contains important keywords i.e. literal,string literal,array literal and dereferencing.Firstly,know these keywords to understand the topic better.

Literal

Literal is a notation for representing a fixed value in source code i.e. 1 is an integer literal , ‘B’ is a character literal and true is a boolean literal.

String Literal

A string literal is a group of characters enclosed within the double quotes i.e. “Hello” is a string literal with the value Hello, quotes are not part of the value.
It can be specified in four different ways-single quoted,double quoted,heredoc syntax and nowdoc syntax.

Array Literal

An Array literal is a collection of elements(values) enclosed within the square brackets and each element is separated by comma and each value is identified by at least one array  index or key.For eg:- [“Anil”,”Udit”,”Dev”] is an array literal.

Reference and Dereference

In PHP,  References means to access the same data such as variable content by different names.They are neither actual memory address nor pointers.Variable name and its content are different things,so the different names can refer to the same content.
The referencing means to refer to or point to the data and accessing that data is called dereferencing the reference.
It is possible to directly access the element of an array returned by the function call without storing it in the temporary variable because dereferencing of array elements from the result of function and method calls is supported in PHP 5.4.
And now in PHP 5.5, an Array and String literals can be dereferenced directly to access individual elements of the array and characters of the String.

Syntax:

Let’s understand the syntax with the help of an example:

<br />
&lt;?php<br />
 // array dereferencing - returns 5<br />
 echo &quot;Element on zero index is: &quot;;<br />
 echo [ 5, 7, 8, 9][0];<br />
 echo &quot;\n&quot;;<br />
 // string dereferencing - returns &quot;!&quot;<br />
 echo &quot;Element on fifth index is: &quot;;<br />
 echo &quot;Hello! everyone.&quot;[5];<br />
 echo &quot;\n&quot;;<br />
?&gt;<br />

 

OutPut:

Element on zero index is: 5
Element on fifth index is: !

We can directly access an array element or a character of a string  by writing its index in square brackets besides that array or string.As you can see in the above example. Usage:

  • In previous versions we can not get the element on particular index of anonymous array or string.Now,this feature helps to get that element.
  • Eliminates the extra variables for assignment: As per the given Example below, it eliminates extra variables that previously required to hold an array or to hold an array index.

Example 1:

Get the random name from the anonymous array of names.

<br />
&lt;?php<br />
 function getName() {<br />
 return [&quot;Anil&quot;,&quot;Udit&quot;, &quot;Dev&quot;][mt_rand(0,2)];<br />
 }<br />
 echo getName();<br />
?&gt;<br />

The first function does not need any variable to hold an array.It just returns the random name.

<br />
&lt;?php<br />
 function getName(){<br />
 $input = array(&quot;Anil&quot;,&quot;Udit&quot;, &quot;Dev&quot;);<br />
 $rand_keys = array_rand($input);<br />
 return $input[$rand_keys];<br />
 }<br />
 echo getName();<br />
?&gt;</p>
<p>

The output of the two functions will be same,both will return the random name from the given array.But in second function,we need a variable to hold an array and second variable is used to hold  the random index returned by array_rand(),then It returns the random name from the given array.

  • This feature reduces the code by eliminating the extra variables.

Example 2:

Get the random String of 10 character

<br />
&lt;?php<br />
 function getRandomChar(){<br />
 return &quot;0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;[mt_rand(0,61)];<br />
 }</p>
<p> function generateRandomString($length = 10) {<br />
 $randomString = '';<br />
 for ($i = 0; $i &lt; $length; $i++) {<br />
 $randomString .= getRandomChar();<br />
 }<br />
 return $randomString;<br />
 }<br />
 echo generateRandomString();<br />
?&gt;<br />

 

Output:

aBeR3Ws9q1

the above output will be different every time we use the above function.

  • This feature is useful in some places where we need to use references.Look at the  example given below:

Example 3:

<br />
&lt;?php<br />
 $data = array('good', 'better', 'best');<br />
 function &amp;getData() {<br />
 return $GLOBALS['data'];<br />
 }<br />
 getData()[2] = 'rest'; // $data will now contain 'good', 'better' and 'rest'<br />
 echo getData()[2];<br />
 echo &quot;\n&quot;;<br />
 print_r($data);<br />
?&gt;</p>
<p>

 

Output:

rest
Array
    (
     [0] => good
     [1] => better
     [2] => rest
    )

Conclusion:

This feature is not going to change the way of writing PHP scripts. It only improves the language consistency.

Udemy Generic 728x90

Spread the word. Share this post!