[PHP] empty function and array indices
Hello! Suppose I have the following code: $a = array('a'=1, 'b'=2); echo empty($a['c'])?'empty':'not empty'; echo $a['c']; Why doesn't the 2nd line output a warning when error_reporting is set to E_ALL? Is empty() some kind of special function where the validity of indices is not checked? If that is the case, I have two questions: 1. Will this behavior persist in future versions of PHP? 2. Are there other functions or a general rule where PHP doesn't output warnings when a nonexistant index is given? -- Immanuel doesn't pun, he Kant. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] empty function and array indices
Gabriel Birke wrote: Hello! Suppose I have the following code: $a = array('a'=1, 'b'=2); echo empty($a['c'])?'empty':'not empty'; echo $a['c']; Why doesn't the 2nd line output a warning when error_reporting is set to E_ALL? Is empty() some kind of special function where the validity of indices is not checked? If that is the case, I have two questions: 1. Will this behavior persist in future versions of PHP? Sure 2. Are there other functions or a general rule where PHP doesn't output warnings when a nonexistant index is given? isset() as well for obvious reasons. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] empty function and array indices
From the Manual: empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set It appears that there is a revamp of what is considered to be empty in PHP 5. http://php.net/empty You might be better served with array_key_exists() such as: echo array_key_exists('c', $a)? 'empty':'not empty'; Mark Cain - Original Message - From: Gabriel Birke [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Sunday, May 01, 2005 4:47 PM Subject: [PHP] empty function and array indices Hello! Suppose I have the following code: $a = array('a'=1, 'b'=2); echo empty($a['c'])?'empty':'not empty'; echo $a['c']; Why doesn't the 2nd line output a warning when error_reporting is set to E_ALL? Is empty() some kind of special function where the validity of indices is not checked? If that is the case, I have two questions: 1. Will this behavior persist in future versions of PHP? 2. Are there other functions or a general rule where PHP doesn't output warnings when a nonexistant index is given? -- Immanuel doesn't pun, he Kant. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] empty function and array indices
On Sun, May 1, 2005 1:47 pm, Gabriel Birke said: Is empty() some kind of special function where the validity of indices is not checked? Bingo! That's rather the whole point of empty and isset. :-) The behaviour of empty has changed over time, with various releases. isset seems more stable in that regard, so I prefer to use that one. Also, to me, philosophically and from an application logic perspective, I generally want to do something different when some variable is not set at all, as opposed to having in it. But that's maybe just me. :-) YMMV -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php