On 14 April 2011 09:59, Eloy Bote Falcon <eloyb...@gmail.com> wrote: > What is the purpose of that generateHash function? It doesn't work in the > isset check. > > Anyway, you can do a simple $a = array('foo'); isset($a[$x][$y][$z]) without > notices at all unless any of $x $y or $z are not defined, you don't need to > check the indexes one by one. > > 2011/4/14 Ole Markus With <olemar...@olemarkus.org> > >> On Thu, 14 Apr 2011 02:24:56 +0200, Ben Schmidt < >> mail_ben_schm...@yahoo.com.au> wrote: >> >> I think these shortcuts could be really useful for array elements, but >>>> for other variables I am really sceptical and I think they would do >>>> more harm than good. Generally I do not really see any reason why a >>>> variable should not be 'instanciated' before use. >>>> >>>> So >>>> +1 if this shortcut is implemented to only work for array elements. >>>> -1 for any other variable type. >>>> >>> >>> There are two issues here. >>> >>> 1. Suppression of notice. I agree, it is best done only for array >>> keys. It's not hard to initialise a variable with $var=null at the >>> beginning of a code block to avoid such a notice, and that is the >>> appropriate way to do it for variables. >>> >>> 2. Offering a shortcut for the common idiom isset($x) ? $x : $y in >>> line with the DRY design principle. A notice would never be emitted >>> here in any case. The problem is that this idiom is still in wide use >>> despite the shortcut ternary operator already provided, because an >>> isset() check is different to a boolean cast. >>> >>> Some thoughts: >>> >>> - The actual intent of 2. is probably $x!==null ? $x : $y i.e. it's >>> not about suppressing notices at all, but about offering a default >>> value, and the idiom quite probably only uses isset() because it >>> predated null in the language. >>> >>> >> I have never felt the need for a shortcut in these cases. It would be >> interesting to see some code where this would be practical. >> >> >> - If we view 2. in this way, the two problems are independent, and it >>> seems to me it would be best to solve them independently, rather >>> than with a single operator. >>> >>> So, I suggest: >>> >>> 1. An array lookup mechanism that suppresses the notice for undefined >>> keys. It would work the same as regular array index lookups except >>> that the notice for undefined keys (and only for undefined keys) >>> would not be generated (it would not just be hidden, but would never >>> be even generated). >>> >> >> This is what I feel PHP is missing. Particularly when it comes to >> multi-dimensional arrays. Because this feature is missing I tend to do >> something like >> >> function generateHash($x, $y, $z) >> { >> return "$x::$y::$z"; >> } >> >> if (isset($a[generateHash($x, $y, $z)]) { >> ... >> } >> >> instead of >> >> if (isset($a[$x]) && isset($a[$x][$y]) && isset($a[$x][$y][$z])) { >> ... >> >> } >> >> Arguing about syntax is then a second step. However, my views on this >>> are: >>> >>> >> I think it best to avoid discussing the actual syntax before agreeing on >> what we really need. >> >> -- >> Ole Markus With >> Systems Architect - Sportradar AS >> Developer - Gentoo Linux >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >
http://news.php.net/php.internals/51877 array_key_exists($key, $array) for arrays array_key_exists($varname, get_defined_vars()) for locally scoped variables. No need to use @. isset() and empty() will not differentiate between an undefined variable or index and a variable or an array element which is assigned null. The E_NOTICE only ever gets fired for the undefined variable/key ... Notice: Undefined variable Notice: Undefined index I think it depends upon the developer's requirement. Are they attempting to determine the existence of a variable/index entry or are they attempting to determine if the variable/element is null. I always declare my variables. So, I don't want to use isset() as they will be an incorrect test. I use is_null(). Specifically testing the value. If I've made a mistake and NOT declared the variable (or made a typo), I want to know. I don't want to hide it with isset()/empty(). -- 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