Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70192 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46979 invoked from network); 18 Nov 2013 14:40:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Nov 2013 14:40:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=indeyets@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=indeyets@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.178 as permitted sender) X-PHP-List-Original-Sender: indeyets@gmail.com X-Host-Fingerprint: 209.85.215.178 mail-ea0-f178.google.com Received: from [209.85.215.178] ([209.85.215.178:38138] helo=mail-ea0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 53/01-41213-1E62A825 for ; Mon, 18 Nov 2013 09:40:34 -0500 Received: by mail-ea0-f178.google.com with SMTP id d10so2218791eaj.37 for ; Mon, 18 Nov 2013 06:40:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:openpgp:content-type; bh=HQs4J+REXPmBTNNQqrncxigOanF0qf674FS0JrQ6U70=; b=Nsj4PE7T/HTqjA0/r/B/uIFT1QCRUtNtdjvAykhGNjmrGxFVIfKDqrzx+EPUkrv1PW xsbeVR2X3N9QvHAMyaopY2bROgHqUH5ztY9z7TY8uxxc68JYfUYe9E6L96nvXf9dU/hr xF81wRVCVDPkxxU7+NcLV2f2P++6TOIBSuGFCKJHrSUaL0lkRFyzPssVLNYLX6nNSMIp /LOKwxe+cQwCiFDv5tQGsW6BujbSOCZ8xpekds0oyFSyOeVTJCkQFEaCprXhICfDt8dA 1rNpxV5zRDeSrkhmuzjiUrMRg8OhnTV9Y1rNXgkfVu6wlt6VHyiSBvPu1SdMtSt5rM54 pXbw== X-Received: by 10.14.172.133 with SMTP id t5mr13304504eel.35.1384785631250; Mon, 18 Nov 2013 06:40:31 -0800 (PST) Received: from Gearder.local ([92.255.5.146]) by mx.google.com with ESMTPSA id k7sm38485396eeg.13.2013.11.18.06.40.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 18 Nov 2013 06:40:29 -0800 (PST) Message-ID: <528A26D2.6020609@gmail.com> Date: Mon, 18 Nov 2013 18:40:18 +0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 MIME-Version: 1.0 To: Nicolai Scheer , PHP internals References: In-Reply-To: X-Enigmail-Version: 1.6 OpenPGP: id=1D219F11 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="f8sbrRJGsW5a4WppeSTxhaETuNtQR2peJ" Subject: Re: [PHP-DEV] foreach and current() From: indeyets@gmail.com (Alexey Zakhlestin) --f8sbrRJGsW5a4WppeSTxhaETuNtQR2peJ Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 18.11.13, 18:14, Nicolai Scheer wrote: > Hi all! >=20 > Has it been always the way, that it is not possible to use current() in= a > foreach loop? >=20 > Just stumbled upon (using php 5.5.5): >=20 > $arr =3D array( "a", "b", "c" ); >=20 > foreach( $arr as $entry ) > { > #var_dump( current($arr) ); > } >=20 > var_dump( current($arr) ); >=20 > Which yields "false", because the internal array pointer points behind = the > last element of $arr. > If I uncomment the inner var_dump(), the script outputs "b" four times.= > This seems odd... I know that the documentation advises not to fiddle w= ith > the internal array pointer in a foreach loop, but current() should not > affect it in any way and therefor should be safe to use. >=20 > Does anyone know what the reasoning behind this behavious is? current() implementation predates foreach and is totally separate. Main difference is, that current()'s "pointer" is attached to the array, and foreach's pointer is external. so you can not run several each()/current() loops on array at the same time, but you can have any number of nested foreach's iterating over the same array independently. They can't really be combined because of backwards compatibility --=20 Alexey Zakhlestin CTO at Grids.by/you https://github.com/indeyets PGP key: http://indeyets.ru/alexey.zakhlestin.pgp.asc --f8sbrRJGsW5a4WppeSTxhaETuNtQR2peJ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Comment: GPGTools - http://gpgtools.org iQEcBAEBCgAGBQJSiibcAAoJEMkJcRxZdR27u8UH/Rsm/H9lQbTyxJzdkqsnRFz/ u1C/+nEwXlu51rVZ91emsITlxM5EDHXE3ld1jIXFshp0HqxphKB8gV0uozUcy7nW A86a4iyz12dLy1PAa6ZIPQfYXQ20pmErRQZiRKBIdeKkg+OwHOTzJeO9j+uWNfkI ayhXIZ78k2VTb1NdR2Tz+TL8wxefKkm4/+MJycAY+KRgzUtnVZOv7A8HVxDpinE5 Na49P9RxokONKzOqATn+1aI5xLOrsGxPZhMIrTzSR1Bce3AxQow0BNX7N7jUGadw W4ZxP22wpnK5NUHfyByjDuI24kF81Cfvg6edZWJ6Pj05/iW0urJDT1Z2ng84Nvs= =ED/e -----END PGP SIGNATURE----- --f8sbrRJGsW5a4WppeSTxhaETuNtQR2peJ--