Tuesday 26 December 2017

arguments - Reference — What does this symbol mean in PHP?

QUESTION:


What does
=> mean?


/>

ANSWER:


=> Is the symbol we humans
decided to use to separate "Key" => "Value" pairs in
Associative Arrays.


ELABORATING:


To
understand this, we have to know what Associative Arrays are. The first thing that comes
up when a conventional programmer thinks of an array (in
PHP
) would be something similar
to:


$myArray1 = array(2016, "hello",
33);//option 1
$myArray2 = [2016, "hello", 33];//option 2
$myArray3
= [];//option 3
$myArray3[] = 2016;
$myArray3[] =
"hello";
$myArray3[] =
33;

Where as, if we wanted to
call the array in some later part of the code, we could
do:


echo $myArray1[1];// output:
hello
echo $myArray2[1];// output: hello
echo $myArray3[1];//
output: hello

So far so good.
However, as humans, we might find it hard to remember that index
[0] of the array is the value of the
year 2016, index [1] of the array
is a greetings, and index [2] of
the array is a simple integer value. The alternative we
would then have is to use what is called an Associative
Array
. An Associative array has a few differences from a
Sequential Array
(which is what the
previous cases were since they increment the index used in a predetermined sequence, by
incrementing by 1 for each following
value
).


Differences (between a
sequential and associative array
):


readability="2.5">
  • Durring the
    declaration of an Associative Array, you don't only include the
    value of what you want to put in the array, but you also put
    the index value (called the key) which you want to use when
    calling the array in later parts of the code. The following syntax is used during it's
    declaration: "key" => "value".


  • When using the
    Associative Array, the key value would then be placed inside
    the index of the array to retrieve the desired
    value.



  • For
    instance:


     $myArray1 = array(

    "Year" => 2016,
    "Greetings" => "hello",
    "Integer_value"
    => 33);//option 1
    $myArray2 = [
    "Year" => 2016,

    "Greetings" => "hello",
    "Integer_value" => 33];//option 2

    $myArray3 = [];//option 3
    $myArray3["Year"] = 2016;

    $myArray3["Greetings"] = "hello";
    $myArray3["Integer_value"] =
    33;

    And now, to receive the same
    output as before, the key value would be used in the arrays
    index:


    echo $myArray1["Greetings"];// output:
    hello
    echo $myArray2["Greetings"];// output: hello
    echo
    $myArray3["Greetings"];// output:
    hello

    FINAL
    POINT:


    So from the above example, it is
    pretty easy to see that the => symbol is used to express the
    relationship of an Associative Array between each of the key
    and value pairs in an array DURING
    the initiation of the values within the array.

    No comments:

    Post a Comment

    php - file_get_contents shows unexpected output while reading a file

    I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...