[wp-hackers] How do filters access variables outside of their function?

Andrew Nacin wp at andrewnacin.com
Fri Aug 27 21:57:13 UTC 2010

On Fri, Aug 27, 2010 at 5:44 PM, Ken <Ken at adcstudio.com> wrote:

> Is there better Example code out there? I'd rather do it cleaner then @
> suppression...

The proper way to check for whether indices are set would be the use
isset(), empty(), or array_key_exists(). Here's a breakdown of when to use
which. I personally use empty() more than I use isset(), and only use
array_key_exists() for the rare use case.

Use isset() when you need to know that the index is set, regardless of its
  - Examples: $_GET['number'] = true; $_GET['number'] = false; In both
cases, isset($_GET['number']) is true.
  - Use case: When you need to check simply for the index, and not its

Use empty() when you need to know that the index is set and evaluates to
true. Generally I use !empty() as it complements isset().
 - Example: $_GET['number'] = true; empty($_GET['number']) is false.
 - Example: $_GET['number'] = false; empty($_GET['number']) is true.
 - Example: $_GET['number'] is not set; empty($_GET['number']) is true.
 - Good use case: Checkboxes only submit data when checked. This works
perfectly: $was_box_checked = !empty($_GET['checkbox']);

Only use array_key_exists() for a specific use case, an exception to how
isset() works with null values.
 - Problem: isset($array['number']) returns false, if $array['number'] is
'set' but its value is NULL.
 - Solution: array_key_exists( 'number', $array ).

More information about the wp-hackers mailing list