Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62622 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9500 invoked from network); 1 Sep 2012 09:07:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2012 09:07:35 -0000 Authentication-Results: pb1.pair.com header.from=theanomaly.is@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=theanomaly.is@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: theanomaly.is@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:50020] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1B/D0-03671-550D1405 for ; Sat, 01 Sep 2012 05:07:34 -0400 Received: by lahl5 with SMTP id l5so2780922lah.29 for ; Sat, 01 Sep 2012 02:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=/OQKuVwu9ndPHKKMl16lpRjnG8KGAWuRTNYWJYbWIcc=; b=UX7EVHkE1QI/fgkKR/alYo/1zQriwNTem9HgyL1DU7eU6yinkyJw+NlYup2GNkwna0 tjWZgmynvCl9K4/bUnT5bbooD0kQwsrxjCyCV1r5zPeHwTDo/Q0XpFUExokM26KwXrF2 +UNiJG5aFPwfsZ4d813iPbqqeXsrIUwxF5gTykSBe3r/q2cn1GIwa9X8UwoPko++820O vf+0poKhJmcki8L/2SfF7s5xnl2flNgc66IlDBgqgUx+dY8oCqKPYwLOxvFd8ST+V/jD J/4RLzhHlnMrqjD1OrqMBO5eXDSfLlYuyy7OO8JCfdJrJY/I+U6nM8/5hr3tYCeKZkUn DnWw== MIME-Version: 1.0 Received: by 10.112.88.2 with SMTP id bc2mr1642597lbb.61.1346490450522; Sat, 01 Sep 2012 02:07:30 -0700 (PDT) Received: by 10.112.8.7 with HTTP; Sat, 1 Sep 2012 02:07:30 -0700 (PDT) In-Reply-To: References: Date: Sat, 1 Sep 2012 05:07:30 -0400 Message-ID: To: Kris Craig Cc: Tjerk Meesters , Rasmus Schultz , Ferenc Kovacs , "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Support negative indexes for arrays and strings From: theanomaly.is@gmail.com (Sherif Ramadan) > > This discussion kinda reminds me of some of the debates over AUTO_INCREMENT > behavior in the MySQL community. Specifically, they end up having to tackle > the same funcamental, conceptual dilemma: If I assign/insert/whatever an > arbitrary value to a container that can be incremented, and then I direct > said container to generate the next increment, what value should that be? > What's the most sensible (or perhaps the least unsensible) way to determine > that? > > Of course, I don't really know what the answer is. From a strictly logical > standpoint (ignoring the fact that it just doesn't work this way in PHP), my > thinking would be that the count-based index approach makes the most sense. > I think many (if not most) PHP developers incorrectly assume this to already > be the case now. > > What's my point? I'm not sure that I have one-- but if I did, the point > would be that any approach to handling this will likely be problematic in > some way or another. That said, I think we should find a way to support > negative indexes. There were times when being able to do that would have > been very convenient for me. =) > > --Kris > I really think people are misunderstanding both the problem and the solution here so I will try to make this very simple. * Problem: Finding an element in a PHP array by its offset * Solution: array_slice($array, -1) * Confusion: Array offsets are not indexes or array keys * Deep confusion: $array[-1] can be used to access an element by its offset * Twilight Zone: PHP Arrays are ordered by their keys The problem here is not that PHP doesn't support or allow you to access the element in an array by its offset or that a negative offset can't reach into the bottom/end of the array. The problem is that people are confusing an offset with an index. PHP arrays have keys. The $array[] syntax allows us to access an array element by its key, not by its offset. The reason is simple: PHP Arrays are ordered hashmaps (they are made up of ordered key/value pairs). To confuse people now by modifying this syntax to allow you to do what array_slice() already does perfectly well, would make using the same syntax for accessing array values by their keys impossible.