Considering that the main impetus for these threads is to write code
that does not generate the notice regarding missing variables or
indices, neither isset() or empty() will provide that completely.

If a variable is declared, but assigned null, it is not set and it is
empty. But so what. The variable exists and will not generate a notice
if access is attempted. No suppression of the non existent notice is
necessary.

The issue is one of undefined variables and indices. That's what the
E_NOTICE says ...

Notice: Undefined variable
Notice: Undefined index


To directly detect the presence of a key, then array_key_exists() is
the function that covers the requirement.

Global variables are easily detectable using array_key_exists($key, $GLOBALS);

Properties for an object ... property_exists().

For locally scoped variables, the function get_defined_vars() can be
combined with array_key_exists().

But the obvious overhead of having to extract all the variables into a
temp array is probably going to be a performance no-no.

The script below (and in [1] in case the formatting all goes wonky),
demonstrates this.

Ideally, a construct that specifically detects if a variable is
declared, irrespective of its value, would be the perfect solution as
this could be combined with isset() or empty() as the developer needs.

Richard.

[1] http://pastebin.com/qLNYtfAw

<?php
error_reporting(-1);

function report($desc, $isset, $empty, $defined) {
  return
    $desc .
    '  isset() = ' . ($isset   ? 'true ' : 'false') .
    '  empty() = ' . ($empty   ? 'true ' : 'false') .
    '  defined = ' . ($defined ? 'true ' : 'false') .
    PHP_EOL;
}

function tester() {
  // $undefined_variable = '';
  $defined_variable_null_value = null;
  $defined_variable_value = 'non null variable';
  $array = array(
   // 'undefined_key' => '',
   'defined_key_null_value' => null,
   'defined_key_value' => 'non null element',
  );

  $defined_vars = get_defined_vars();

  echo
    report('Undefined variable              ',
      isset($undefined_variable),
      empty($undefined_variable),
      array_key_exists('undefined_variable', $defined_vars)),

    report('Defined variable null value     ',
      isset($defined_variable_null_value),
      empty($defined_variable_null_value),
      array_key_exists('defined_variable_null_value', $defined_vars)),

    report('Defined variable non-null value ',
      isset($defined_variable_value),
      empty($defined_variable_value),
      array_key_exists('defined_variable_value', $defined_vars)),

    report('Undefined key                   ',
      isset($array['undefined_key']),
      empty($array['undefined_key']),
      array_key_exists('undefined_key', $array)),

    report('Undefined key null value        ',
      isset($array['defined_key_null_value']),
      empty($array['defined_key_null_value']),
      array_key_exists('defined_key_null_value', $array)),

    report('Undefined key non-null value    ',
      isset($array['defined_key_value']),
      empty($array['defined_key_value']),
      array_key_exists('defined_key_value', $array));
}

tester();
?>
-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to