Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:27215 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13133 invoked by uid 1010); 29 Dec 2006 22:04:39 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 13118 invoked from network); 29 Dec 2006 22:04:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Dec 2006 22:04:39 -0000 Authentication-Results: pb1.pair.com header.from=jochem@iamjochem.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=jochem@iamjochem.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain iamjochem.com from 194.109.193.121 cause and error) X-PHP-List-Original-Sender: jochem@iamjochem.com X-Host-Fingerprint: 194.109.193.121 mx1.moulin.nl Linux 2.6 Received: from [194.109.193.121] ([194.109.193.121:58752] helo=mx1.moulin.nl) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 56/50-09304-6F095954 for ; Fri, 29 Dec 2006 17:04:39 -0500 Received: from localhost (localhost [127.0.0.1]) by mx1.moulin.nl (Postfix) with ESMTP id 4855D244914; Fri, 29 Dec 2006 23:04:44 +0100 (CET) X-Virus-Scanned: amavisd-new at moulin.nl Received: from mx1.moulin.nl ([127.0.0.1]) by localhost (mx1.moulin.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jXTJidPnZS2G; Fri, 29 Dec 2006 23:04:41 +0100 (CET) Received: from [10.0.13.54] (ip129-15-211-87.adsl2.versatel.nl [87.211.15.129]) by mx1.moulin.nl (Postfix) with ESMTP id 69D871FB983; Fri, 29 Dec 2006 23:04:41 +0100 (CET) Message-ID: <459590F0.9070408@iamjochem.com> Date: Fri, 29 Dec 2006 23:04:32 +0100 User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 To: Daniel Convissor CC: PHP Internals List References: <20061229210706.GA14001@panix.com> In-Reply-To: <20061229210706.GA14001@panix.com> X-Enigmail-Version: 0.94.1.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] invalid array key/index not generating notices From: jochem@iamjochem.com (Jochem Maas) without getting involved with whether there is a bug here or not (I neither have the karma or the insight to make that judgement), I would like to offer the idea that an alternative coding style could have averted the problem regardless of the lack of an E_NOTICE... namely a more defensive coding style; some example code to make this clear: maybe this is food for thought for you/your team with regard to defending against [such] mistakes - I know from experience that, even though I am often the only developer on a project, it pays not to make to many assumption about what should be returned from functions/methods. Daniel Convissor wrote: > Folks: > > I came across a subtle bug a developer introduced into our application. > It took us a month to realize the bug was there because PHP didn't throw a > notice. Here is a simplified version of what was happening. > > // function some_func() {} > $a = some_func(); > if ($a['do_something'] == true) { > // Do it. > } > > some_func() was supposed to return an array. But it was returning void. > So $a was NULL. Oops -- we all make mistakes. What's unfortunate is PHP > didn't raise a "Notice: Undefined index: do_something" here. It would > have saved us some headaches. I'm sure others have run into this as well. > > The following also doesn't produce a notice: > > $a = 12; > echo $a['k'] > > I looked through the bugs database and mailing list archive and came up > with nothing on this particular issue. But bugs 29271, 30885 and 38165 > cover the situation where a key's string is auto-converted to 0: > > // While this is a behavior we all truly expect: > $a = 'value'; > echo $a[0] . "\n"; // output: v > > // Another oddity, but people closing bugs say it's expected: > $a = 'value'; > echo $a['k'] . "\n"; // output: v > > This last behavior is counter-intuitive, let alone un-documented. it does become intuitive once you truly understand the way php autocasts stuff - I'll happily admit it took a post from Rasmus (no less) on the generals mailing list for me to start to really understand what goes on in situations such as these... I'll try to explain (and hope I don't do anyone an injustice): 1. $a is a string therefore only integers are allowed as 'keys' in the 'array notation' used for accessing offsets of the string directly 2. the 'key' is string and therefore converted a integer according to the string-to-integer conversion rules... leading consecutive numeric characters are taken to be the integer and in cases where there is no leading numeric character the string converts to zero (not taking into account a leading sign character that might exist in the string e.g. echo (int) "123string"; // 123 echo (int) "1string"; // 1 echo (int) "string"; // 0 > > Wondering what the folks here think about this. > > Thanks, > > --Dan >