Where are all my PHP variables hiding?

Where are all my PHP variables hiding?

16885879.jpg

Sometimes it's difficult to remember what variables you have available to you at any given time, especially if your script is split into functions, or in my case you're working with Drupal, where there's a whole slew of variables available at your disposal, if only you know what they were called. Lucky for curious folks like us, there's a couple native PHP functions that will clear things up:

//Returns an array of all variables and their values. Sweet!
get_defined_vars();

//Prints out the contents of whatever variable you pass to it.
var_dump();

The killer combo

This will print out all the variables currently available to you, and then kill the script so you can just look at the output. Var_dump() prints out the variables in text format, so you'll find it a bit easier to check out your browser's "view source" to see the spacing. Sometimes, when I'm tracking a deep variable I'll copy the code and paste it into Dreamweaver, where I can measure the indents.

I'm also hoping this entry will help me keep track of those function names, because it's mentally strenuous to articulate to Google exactly what the function I'm looking for does. "Output all PHP variables .... in some format I can read ... please?"

Discussion on this post:
Implied By Design Logo