0

I'm trying to patch a bit of PHP in a rather large and complex WordPress theme, and I found this line:

if(is_array($taxonomys)) {
    $tax = 1;
    foreach ($taxonomys as $key => $value ) {

        if($taxonomys[$key]->parent == 0 && isset($taxonomys[$key]->name) && in_array($taxonomys[$key]->name, $used_taxonomys)) {
            echo '<li class="tab"><li class="selected"><a href="#" data-filter="*" class="selected">'.lambda_translate_meta($taxonomys[$key]->name).'</a></h3></li>';
        } 
        if(in_array($taxonomys[$key]->name, $used_taxonomys) && $taxonomys[$key]->parent != 0 ) {                           
            echo '<li class="tab"><h3><a href="#" data-filter=".'.$taxonomys[$key]->slug.'_filt">'.lambda_translate_meta($taxonomys[$key]->name).'</a></h3></li>';
        }
    $tax++;
    }
}

Could someone please explain the -> and => syntax? Google is surprisingly unhelpful.

As a side note, if anyone would also offer there ideas about what the if statements are about, I would be incredibly grateful.

technillogue
  • 1,482
  • 3
  • 16
  • 27
  • 3
    This really is basic PHP which is handled perfectly fine by reading the manual: http://php.net/manual/en/control-structures.foreach.php, http://php.net/manual/en/language.types.object.php and http://php.net/manual/en/langref.php in general – PeeHaa Jun 13 '13 at 21:18
  • You can replace all occurrences of `$taxonomys[$key]` with `$value`. – David Harkness Jun 13 '13 at 21:18
  • Besides Google, there is also Symbolhound for searching source code tokens, http://symbolhound.com/?q=php+what+does+%3D%3E++mean%3F – mario Jun 13 '13 at 21:21
  • Upon reflection this question ought to be closed as a duplicate of several other questions. – technillogue Jun 13 '13 at 22:09

3 Answers3

3
foreach ($taxonomys as $key => $value ) {

This loops through each element in the $taxonomys array, assigning the key to $key, and the value to $value. This array is a associative array where values have specific keys, not necessarily numeric keys.

$taxonomys[$key]->parent

The -> gets you the property of the object. The object in this case is $taxonomys[$key], and the property they want is parent. Note that they could have also just used $value->parent, since due to the foreach loop, $value is the same as $taxonomys[$key].

Brad
  • 159,648
  • 54
  • 349
  • 530
1

-> is used to indicate a method or property of an object

=> is used either to initialise a key/value pair in an array or, as in this case, to extract the key/value pair in a foreach loop

1

-> denotes accessing a property or method of an object.

=> is generally used to defined array('key' => 'value') pairs in an array. The foreach case is special and it just means that the code inside of the foreach block will be executed once for each element in the $taxonomys array and the element key will be available in the $key variable and the value in the $value variable.

km6zla
  • 4,787
  • 2
  • 29
  • 51