1

I frequently see $var->another_var, or $somevar=>yet_another, or even $third_var->another=>$fourth_var in various snippets of code.

Is there some super amazing info-graphic somewhere that clearly explains the various usages and what they mean, specifically within a PHP context?

(In my case, using Drupal, which uses LOTS of arrays, but probably useful in lots of other CMSs / frameworks.)

EDIT: I have since been informed about a catch-all page that has a very useful, encyclopedic list of various symbols and syntaxes. However, I believe one section NOT covered there is the mix-and-match combo of $var->element=>$anothervar.

2 Answers2

4

Single arrow - T_OBJECT_OPERATOR

->

This is used for access to an object property and the value associated with that property.

$object->property='value'

I have a dog and his name is Captain

$dog->name='Captain';

Now I have access to properties of my dog. The property that we have set is name

$dogName=$dog->name;
echo $dogName;

Will output: Captain

I can also add other properties and their associated value to my object.

$dog->weight='57lbs';

Now my object has two properties associated with it, name and weight.

Double arrow - T_DOUBLE_ARROW

=>

As is stated in the documentation an array is just a map of comma separated keys and the values associated with the key. The double arrow is essentially an assignment operator that assigns, or associates, the value to a key.

$array = array("key" => "value");

Again using the dog example.

$dog = array("name" => "Captain", "weight" => "57lbs");

And we can access values in my dog array by the respective keys.

$fatDog = $dog["weight"];
echo $fatDog;

Will output: 57lbs

Combinations of single and double arrow

$object->property=>$value;

This combines object/property with key/values. If we break it down into it's constituents it can make things much more clear.

We know that $object->property will yield the value associated with the property. Lets start by associating that with a variable:

$valueAssociatedWithProperty = $object->property;

Using substitution into the original gives:

$valueAssociatedWithProperty => $value;

We have seen that before it is just the key/value of an array! Lets apply this to the dog example and see what comes out:

$dog->name="Captain";
$description="He is crazy";
$array = array($dog->name => $description);
// $array = array("Captain" => "He is crazy");
$whatIsCaptain = $array["Captain"];
echo $whatIsCaptain;


He is crazy

I hope this helps.

Also look HERE for all the references you could ever hope for!

Community
  • 1
  • 1
Jeremy
  • 530
  • 8
  • 19
2
  • $var->another_var is "property another_var of object referenced by $var".

  • $somevar=>yet_another is used in array definitions, like this: $arr = array($somevar => yet_another). It would define an associative property with key equal to the value of variable $somevar, and value equal to the constant yet_another.

  • $third_var->another=>$fourth_var can be rewritten so it becomes more clear:

    array(  /*key=*/ ($third_var->another)  =>  /*value=*/ $fourth_var  )` 
    
Alex Shesterov
  • 21,630
  • 10
  • 65
  • 88