Get all defined variables in PHP

I’ve never actually used this function, but could definitely see using it to
help profile a system and/or to help identify defined but unused variables.

To obtain an array of all of the defined variables you would:

$defined = get_defined_vars

The function returns a multi-dimensional array of all of the defined variables.
This is not limited to used-defined variables, so you will end up seeing server
and environment variables as well.

There are also some complementary functions that allow you to get a list of the
defined functions and constants, get_defined_functions() and
get_defined_constants() respectively.

Have you ever used this function or know of a good use for it? Share it in the
comments below, I’d love to hear about it!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.