Title: Template coding practice(function used as user variable)
Last modified: August 19, 2016

---

# Template coding practice(function used as user variable)

 *  Resolved [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/template-coding-practicefunction-used-as-user-variable/)
 * I’m studying the very basics of WordPress template creation and one of the templates
   has me scratching my head. I’m stepping through this block of code and writing
   an explanation of what is going on for my own benifit and then studying the WordPress
   codex to make sure I understand the functions used. I think the code block below
   has a user defined variable named after a WordPress function. It works alright
   but I’m just trying to make sure I don’t get the wrong idea about coding practices
   or maybe I’m confused about the usage.
    In this block of code lines 3 and 4 the
   use of $category is what I’m questioning.
 *     ```
       $categories_list = get_categories('hide_empty=0&orderby=name');
       $getcat = array();
       foreach($categories_list as $category){
       	$getcat[$category->cat_ID] = $item->cat_name;
       }
       $category_dropdown = array_unshift($getcat, "Choose a category:");
       ```
   
 * I appreciate any guidance and light someone could shed on this issue.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/template-coding-practicefunction-used-as-user-variable/#post-1291368)
 * That’s normal for a [foreach loop](http://php.net/manual/en/control-structures.foreach.php).
   Take line three– starts with ‘foreach’. That reads something like “for each item
   in the array (or object in PHP5) named $categories_list assign the value to the
   variable named $category and loop through using that value”. Then inside the 
   foreach you can access `$category` as you would any other variable. Of course,
   the value changes with every iteration. I hope that makes sense.
 *  Thread Starter [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * (@tsalagi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/template-coding-practicefunction-used-as-user-variable/#post-1291572)
 * That helps a lot. Thanks apljdi

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Template coding practice(function used as user variable)’ is closed to
new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [user-defined](https://wordpress.org/support/topic-tag/user-defined/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * 2 replies
 * 2 participants
 * Last reply from: [tsalagi](https://wordpress.org/support/users/tsalagi/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/template-coding-practicefunction-used-as-user-variable/#post-1291572)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
