Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99873 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10050 invoked from network); 14 Jul 2017 20:40:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2017 20:40:31 -0000 Authentication-Results: pb1.pair.com header.from=tendoaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tendoaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.174 as permitted sender) X-PHP-List-Original-Sender: tendoaki@gmail.com X-Host-Fingerprint: 209.85.217.174 mail-ua0-f174.google.com Received: from [209.85.217.174] ([209.85.217.174:35785] helo=mail-ua0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 49/42-01782-E3C29695 for ; Fri, 14 Jul 2017 16:40:30 -0400 Received: by mail-ua0-f174.google.com with SMTP id j53so58271215uaa.2 for ; Fri, 14 Jul 2017 13:40:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=JdtpObUSYcDKO44UI7i8GNHd27bAOt71EiUZTxBEJHs=; b=ea0YlRjKeZ1a570noSAAZen3/rq7yr4vhB+uK7V2LyKttuYplWdMkeKuPO3lCRMm7L uQWRZ5bYGdk/08eitHLY0U/OjqFK/JZRweY93tMpoSJbISjRXcUBpvhrRpRbAE8ZY+Up Z59mILnjGSgBQ3uSE8IfN/dKCEGQRkL7zDQ+KBNszx5GQLXFAmb9pw86CxnJfJUl1v36 fohG1SKSzs3E4GyaT+HF8jGRAUiEPkGOctXVOw4ZjAQ/+i3cqQlaENlXukmeIX8qUm1q 3z+CJta2ISsm3kWCZAoSasf9Cv3c2BgIZvEbbNjrJNcI9dEXuXA+omVkkyctQ6d5sWr3 GCkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=JdtpObUSYcDKO44UI7i8GNHd27bAOt71EiUZTxBEJHs=; b=UgoGteA6mJcwZpKZes1Hf7gxBtgA/aY2gNCa7uoZlIdJbAF0qiCPqBsbW1RRuKhgmV Ai2JleDrgqPoshMJHxZi/1L5UVolMfiC1o8xjbXSFakvFoYtv52O72iWGIVwcG8Dq7fT L2WmzanEVID9f2zDxpe0oP8gGpt4Zt1bXVF5mFRSvWV8H6wb/CHOUpRX0n/6o7yQ2eKf eD3crP7fOotQpzQalVCsC2gXSMHd0BnTdgkIIGc/j0FKlUqFg1GpuGVA/GxgBA1f4CN3 1NZ5g+yThPNETFY+lx8pa4KghlmfjUy2Rw4+UMy9zRMVoI7n8NdFIyOfJmGXoyzrYa5O JKnw== X-Gm-Message-State: AIVw110ptrnwj427fO/2yDokgUf202Ofzs5/hHuohJrRjUT5eUdraYJ0 pbijGMNJHiEZ+pc8AxNdTYvZxjqWVvZJ X-Received: by 10.159.36.215 with SMTP id 81mr6778343uar.48.1500064827546; Fri, 14 Jul 2017 13:40:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.86.146 with HTTP; Fri, 14 Jul 2017 13:40:27 -0700 (PDT) Date: Fri, 14 Jul 2017 16:40:27 -0400 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="001a113e15c82f9fc505544d0f4b" Subject: [PHP-DEV] Allow else after loop structures for,foreach and while? From: tendoaki@gmail.com (Michael Morris) --001a113e15c82f9fc505544d0f4b Content-Type: text/plain; charset="UTF-8" Reading the array coalesce operator concept thread I thought of this. I think I've seen in another language but I'm not sure. Anyway, here goes... Allow else blocks to follow iteration blocks. The else block is executed if no iteration occurs for any reason. foreach ($array as $key => $value) { echo 'Result ' . $key . ' with value: ' . $value . "\n"; } else { echo 'No results'; } There's no reason why this can't be allowed with for and while as well. Obviously do while loops won't be allowed to use this since they always execute at least once. Like the proposal I was reading when I thought about this is largely syntax sugar to make things slightly easier to read and less verbose than the alternative. if (count($array) > 0) { foreach ($array as $key => $value) { echo 'Result ' . $key . ' with value: ' . $value . "\n"; } } else { echo 'No results'; } However the above existing solution doesn't allow $array to be a traversable but non-countable object. We can add other checks, but the verbosity increases. Back to the issue I was reading when I was reading this, it might be possible to allow foreach/else to take a non array argument without tripping off a warning. In that case the expectation is that the programmer gets to control the response, or elect to have no response with else {} --001a113e15c82f9fc505544d0f4b--