Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60871 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64220 invoked from network); 17 Jun 2012 19:53:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jun 2012 19:53:45 -0000 Authentication-Results: pb1.pair.com header.from=marc@easen.co.uk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=marc@easen.co.uk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain easen.co.uk from 209.85.212.182 cause and error) X-PHP-List-Original-Sender: marc@easen.co.uk X-Host-Fingerprint: 209.85.212.182 mail-wi0-f182.google.com Received: from [209.85.212.182] ([209.85.212.182:34161] helo=mail-wi0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FA/F1-49496-8C53EDF4 for ; Sun, 17 Jun 2012 15:53:45 -0400 Received: by wibhm6 with SMTP id hm6so996315wib.11 for ; Sun, 17 Jun 2012 12:53:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=G/Hg6lDIHNAWlRHM99GP7NGi7jM4UaRG1nV6rTB92nQ=; b=P8ya7dr/yN4+EprIfhJoCGuAOUUOXq8fRKIVdDL0xqAIKP0osYQ1OdwNE9Sh5lPFax YVbrhNWS81keuZ/zcooRyWlkMB8TGAyGkSLjqUjhf7MSO0bGN4oeGfz/MlBpgxVaFSiX wYlPFHDRwWDDzQ6hSuX8S+hAv6OzKuRdr5xFhqjlK+Bx8HSWrErNPnbqPvdNz6JyV8bj +VFmXVlaYQz5NZU8lNHF4+5Nw+36QcYbAC2hIy/y3SwLJjMjt2jbLd/lGxiPVWhyD+Sx M+m1p58yZP67QZIXFV8s1j7nNpD/1GhcLGFCeh6xEHBa+eGxyi5WMsx81lwairqn+Njh b78w== Received: by 10.180.104.228 with SMTP id gh4mr18742121wib.3.1339962821217; Sun, 17 Jun 2012 12:53:41 -0700 (PDT) Received: from [192.168.1.206] (5e0279dd.bb.sky.com. [94.2.121.221]) by mx.google.com with ESMTPS id gv7sm20838160wib.4.2012.06.17.12.53.39 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 17 Jun 2012 12:53:40 -0700 (PDT) Message-ID: <4FDE35C2.9080601@easen.co.uk> Date: Sun, 17 Jun 2012 20:53:38 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120420 Thunderbird/12.0 MIME-Version: 1.0 To: "internals@lists.php.net" , Lars Strojny References: <496E551C-85F0-423E-89FA-2C5245808320@easen.co.uk> <4D309A4D.9020904@sugarcrm.com> <4FCD07CA.7030605@easen.co.uk> <131ff485e2f37a4388919f1c61fd4816.squirrel@www.l-i-e.com> <4FD6436B.1080200@sugarcrm.com> <4FD65CA5.4050703@easen.co.uk> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQkGO5pXHKdLOv0/d7beyX9efJevsvaXA4aPTxSDPVON0G38a2CnAgFK8j0srz1HbkULgEg3 Subject: Re: [PHP-DEV] Support negative indexes for arrays and strings From: marc@easen.co.uk (Marc Easen) Hi Lars, I don't think there needs to be a new operator, as it is possible to achieve this behaviour without adding a new language construct. The odd thing with PHP as I'm sure you are aware of is that arrays can be either a lists or a dictionary. This is where this becomes quite difficult to implement: Numerical keyed array: $a = array('foo', 'bar', 'baz'); $a[0] === 'foo' I would expect: $a[-1] === 'baz'; An string keyed array: $b = array('foo' => 1, 'bar' => 2, 'baz' => 3); $b[0] === 1; Would generate an E_NOTICE: PHP Notice: Undefined offset: 0 in php shell code on line 1. An negative offset would also generate the same E_NOTICE. So going back to your point, the change would only be to numeric based arrays (list) and strings. It could be possible to string keyed arrays to be accessed by the numeric counter parts, but I'm not familiar to comment on if this is even possible. It seems this topic has generated a lot of interest, I feel the best way to proceed would be to write a RFC. I've never written one before so could someone give me access to create a page on the RFC site or create a RFC for me and give me permission to update it, my username is 'easen'. Thanks, Marc On 17/06/12 15:14, Lars Strojny wrote: > Hi Marc, > > Am 11.06.2012 um 23:01 schrieb Marc Easen: > > [...] >>> I don't see much of complex logic here, but $a[2] = 'a' would create a >>> new array element if it does not exist, while $a[-2] can't. Not a big >>> issue, but somewhat inconsistent I guess. >>> >> Please note I'm not referring the negative indexes in arrays, just strings. As I see strings as being simpler to implement and produce a greater benefit compared to supporting negative indexes for arrays. As arrays are more complex structures and adding this behaviour would complicate things somewhat. > I would propose not to try implementing a different behavior for strings than it is for arrays. I think we need a more convenient way to access parts of a string/parts of an array than substr() and array_slice() provide. But this will most likely be a new operator. > > cu, > Lars