Jan 18, 2012 - 1 minute read - php hack

PHP isset with variable name

I wont try to justify PHP hacking but I was working on a project that’s based on CodeIgniter. It’s on a cycle of heavy refactoring. I was wondering if I could do something like


similar to python "variable_name" in globals()

Same idea with locals() in python.

Thankfully, php has the equivalent of globals() being $GLOBALS which would return you all the variables declared so far. Now you can just write your own isset_varname(). get_defined_vars() is equivalent in php to locals(). in python.

you’d probably need to check for the variable name regex and store output from get_defined_vars() in another place too. Of course, if there’s some other similar function that anyone else knows about, I’d be happy to find out.