SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with Breakdown of code block

    I am currently studying PHP and not just how to cut and paste and hack someones code. I have this block of code I want to understand fully.
    It's from a WordPress theme I'm using to help me learn PHP.
    What I don't understand is the foreach loop. Here is what it looks like to my brain. I've included my analysis in comments above each line.

    Code:
    //I understand that $categories_list is a user defined function and is assigned the values that the wordpress function get_categories and it's arguments retrieve.
    $categories_list = get_categories('hide_empty=0&orderby=name');
    
    //I also understand that $getcat is a user defined function that is assigned as an array that will be populated with what the foreach loop does.
    $getcat = array();
    
    //For each value in the $categories_list as(I see the "as" being an assignment similar to =) $category. In effect assigning a variable to a variable that has not yet been defined or a temporary variable. 
    foreach($categories_list as $category) {
    
    //Populate the $getcat array with the temporary variable $category using Wordpress parameters cat_id and cat_name. I don't understand the use of the -> symbols in this line and other places use the => symbol. 
    	$getcat[$category->cat_ID] = $category->cat_name;
    }
    //create a variable $category_dropdown and using the WordPress function 
    array_unshift populate the drop-down with $getcat values with "Choose a category:" at the top.
    $category_dropdown = array_unshift($getcat, "Choose a category:");
    I'd greatly appreciate an analysis of my breakdown and explanation of the sybmols -> and =>.

    Thank you in advance

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2009
    Location
    Bavaria, Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    => assigning a value to a key in an array:
    PHP Code:
    $myarr = array(
      
    => 'x',
      
    => 'y'
    ); 
    -> calling a function or getting a value of an object
    PHP Code:
    class MyClass {
      public 
    $x 'y';
      public function 
    myfunction() {
        echo 
    'Hello world.';
      }
    }
    $MyClass = new MyClass();
    echo 
    MyClass->x;
    $MyClass->myfunction(); 

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks LarsK. Not once in the books I am studying have I had an explanation of those symbols. They just throw them around like noobs are supposed to have prior knowledge of such things.

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand that $categories_list is a user defined function
    wrong
    it's a variable, not a function
    and it's quite complex variable.
    it's an array of objects
    and it means that you've choosen quite complex example to learn
    so, -> is just object programming syntax, for accessing object property.
    note that => is totally different operator, used in foreach loop to retrieve array key along with value

    the rest you've got very well

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2009
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Shrapnel N5. Actually that was a typo. I did mean user defined "variable". I'm glad I chose a complex example because I'm very serious about learning the depths of PHP. I'm breaking down each code block in this theme for starters and then will move on to writing my own. I hope that is a good learning plan. If you have any other ideas as far as how to learn PHP I would love to hear.

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •