1

When writing the syntax for an associative array in PHP we do the following

$a = array('foo' => 'bar');

I am curious of the relationship of the => syntax, or possibly operator. Does this relate to some kind of reference used in the hash table in ZE, or some kind of subsequent right shift or reference used in C? I guess I am just wondering the true underlying purpose of this syntax, how it relates to ZE and/or php extensions used to handle arrays, how it possibly relates to the written function in C before compiled, or If I just have no idea what I am talking about :)

grep
  • 3,986
  • 7
  • 45
  • 67
  • It's just the `key => value` operator. It's used in foreach loops as well when you need the keys as well as the values of the array you're looping on. In Javascript, the equivalent would be `var a = {foo: 'bar'}`. – Marc B Jun 28 '11 at 17:44

2 Answers2

6

The => symbol a.k.a. T_DOUBLE_ARROW is just a parser token like class, || or ::.

See: The list of php parser tokens

It's nothing special apart from that fact that "it looks like an arrow" and it is used for "array stuff".

Of course the exact usage is more complicated than that but "array stuff" is the short inaccurate description that should do it.

It's used to represent key => (points to) value

edorian
  • 38,542
  • 15
  • 125
  • 143
  • 1
    "The => symbol a.k.a. T_DOUBLE_ARROW is just a parser token like = or ||." This is more of what I was looking for. I guess now I just need to research how associative arrays are stored in hash tables. – grep Jun 28 '11 at 17:51
  • @Headspin The word you are looking for when talking about php hash tables usually is `zval`. In php pretty much everything is a hash table but variables are stored in so called zvals. – edorian Jun 28 '11 at 17:53
  • thanks. I am actually looking into the daunting task of writing some PHP extensions which is what lead me to this question. While reading up on zvals specifically ha. – grep Jun 28 '11 at 17:55
4

The answer to that is no simpler than "It looks like an arrow". It's not exactly the assignment operator per say because that would mean a variable-like assignment (like for the array itself). This is an array-internals specific assignment operator.

Webdevelopers are cool like that :P

Angad
  • 2,803
  • 3
  • 32
  • 45
  • Do you know the relationship to this within the hash table used to 'store' the array? – grep Jun 28 '11 at 17:40
  • I don't see any relation, though that definitely doesn't mean there isn't one. But trust me on this, don't use PHP like C. C is beautiful. PHP is hack-patchy-get-the-job-done. What you asked is a classic example of how weird PHP can seem at times. – Angad Jun 28 '11 at 17:44
  • hehe, we both went with "it looks like an arrow" +1 ;) – edorian Jun 28 '11 at 17:48