Önemli yapı endeksi dize tarafından $ _SESSION varolup olmadığını kontrol edin

0 Cevap php

I need to check if a key exists and return its value if it does.
Key can be an array with subkeys or endkey with a value.

$_SESSION['mainKey']['testkey'] = 'value';
var_dump(doesKeyExist('testkey'));
function doesKeyExist($where) {
  $parts = explode('/',$where);
  $str = '';
  for($i = 0,$len = count($parts);$i<$len;$i++) {
    $str .= '[\''. $parts[$i] .'\']';
  }
  $keycheck = '$_SESSION[\'mainKey\']' . $str;
  if (isset(${$keycheck})) {
    return ${$keycheck};
  }

  // isset($keycheck) = true, as its non-empty. actual content is not checked
  // isset(${$keycheck}) = false, but should be true. ${$var} forces a evaluate content
  // isset($_SESSION['mainKey']['testkey']) = true
}

PHP 5.3.3 kullanma.

0 Cevap