I know this one is really old, but there is a room for improvement.
You should consider using $GLOBALS['categories'] instead of just global.
There are two reasons for this:
- We don't have to write
global $categories;
everytime.
- It's crystal clear then we are using global and then not.
Consider this code:
global $categories;
// a lot of PHP code here
<?php print_r ($categories) ?>
Only if we initialize global state right before using variable, it's pretty hard to tell, if it's global or not. And don't forget to repeat it in any of template files you have.
It's possible to use naming conventions for that, but there is a better way, in my opinion.
Consider using $GLOBALS['categories']
.
We only have to initialize our variable one time in functions.php without having to think about global $categories
again. And we can see it's a global one.
print_r ($GLOBALS['categories']);
Performance issue is not a an issue at all in this situation. I'll quote Sara Golemon (link):
What does that mean for your use of the $GLOBALS array? That's right,
the global keyword is technically faster. Now, I want to be really
clear about one thing here. The minor speed affordance given by using
your globals as localized [compiled variables] needs to be seriously
weighed against the maintainability of looking at your code in five
years and knowing that $foo came from the global scope.
something_using($GLOBALS['foo']); will ALWAYS be clearer to you down
the line than global $foo; /* buncha code */ something_using($foo);
Don't be penny-wise and pound foolish..