Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108762 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41678 invoked from network); 25 Feb 2020 22:10:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Feb 2020 22:10:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 243F01804A7 for ; Tue, 25 Feb 2020 12:28:17 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 25 Feb 2020 12:28:16 -0800 (PST) Received: by mail-lj1-f174.google.com with SMTP id q23so368864ljm.4 for ; Tue, 25 Feb 2020 12:28:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=S/cD7mjhpkZ2hsbdiv0vzjXB9qp7sbiyGXJmvI5Hyrk=; b=jS2r/8aZ/rjEttr75catEVPWlg0acN3ZbhUG5YK9JgzH1N8YxrndmpQD581XYzaf73 sald5bK9CPsrzgvi5amRmZM23bydav7IZxA/j0CtoJ2MzrguclfAu9joc6/QYAVXpzI+ e2y9SEDC4J5Ca25wTr7E5KZpwZL9JCYbnrA7hZvWpRs0a7lbCfrHa+hAKjIXcf4SQwoU eAFwGXkSGqv9F7fpweBkma56XWrVteHmJlLLD5G8thlZx/zmI4akEIu0mVUWSLIHO8gq UVVFTrbYcta0q+aFwAIDbRCp58gPmeOSG0d4vFd2WbTzLKKUZgA0wOREmGmjgpqxnfvF Xe0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=S/cD7mjhpkZ2hsbdiv0vzjXB9qp7sbiyGXJmvI5Hyrk=; b=rKgsi7Ar/jr5JihzirWsX8uRXV/6AhemyzRfhKSaX/DGNlLR6MXMRx+n/RUVN4lBEU vIOA2Hh+zjLylh2kRgHv7QMqx79OGnmfFghvUBht6TR6fxLls+xsDQHQjm2fbzxveTCG v1L5fmjg4FPy6gNW1SCd8j0bdUy0hfuob2+4pY4W+3xDzuCyeN+QJT5bgRhsHYK9ZWbx 9hYOblL1wuZzJHfZhiRDe9Erd2eWLyYNCaYhAaYwigrP66LNFw88WSQ6YdX4NGV06naA +RsuUA0SKGRByiAQO3heQQ00P9AgMRDO+v+J7bO5ArbVF7uNqkyctRqPq2QQLD9GRuYe J4eQ== X-Gm-Message-State: APjAAAXcZqkn48vlJf6s676PbheA7ZA5Dk5pDf3+NJ5gWn9IgpzmqUN/ 3sXlSBwflSM47nyhk6Gcn7nFP+pBssCiShGW9nza+u8MoDg= X-Google-Smtp-Source: APXvYqzp64DRVPcDVH8AAa+mFNTs1uYo/TbxINak4JgGc32WI9arHAIhfglAq7ZM63HJTPiv+kgpA75jYs+59PL3RjI= X-Received: by 2002:a2e:a408:: with SMTP id p8mr488107ljn.145.1582662495213; Tue, 25 Feb 2020 12:28:15 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 25 Feb 2020 21:27:58 +0100 Message-ID: To: Michael Morris Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000d39a72059f6c53eb" Subject: Re: [PHP-DEV] Thoughts on adopting Python's for/foreach else construct. From: nikita.ppv@gmail.com (Nikita Popov) --000000000000d39a72059f6c53eb Content-Type: text/plain; charset="UTF-8" On Tue, Feb 25, 2020 at 8:19 PM Michael Morris wrote: > I don't remember if this has been discussed before, but I've been working > with some listing code and wishing for a cleaner way to do this > > if (count($a) === 0) { > // code for no results > } > else { > foreach( $a as $key => $value) { > // code for iteration > } > } > ?> > > How difficult would it be to make the following work in the interpreter? > > foreach($a as $key => $value) { > // code for iteration > } > else { > // code for no results > } > ?> > > The code of the else clause executes if the foreach is never entered > regardless of the reason (provided the code didn't outright crash) > > Thoughts. > See https://wiki.php.net/rfc/loop_else and https://wiki.php.net/rfc/loop_or for previous proposals on the topic. I believe an important concern from previous discussions was that this breaks BC in a really bad way, due to the dangling else conflict. Which is why the latter proposal used "or" instead of "else". Regards, Nikita --000000000000d39a72059f6c53eb--