Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62033 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91617 invoked from network); 4 Aug 2012 19:31:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Aug 2012 19:31:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 64.22.89.134 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.134 oxmail.registrar-servers.com Linux 2.6 Received: from [64.22.89.134] ([64.22.89.134:33695] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/F0-19861-0887D105 for ; Sat, 04 Aug 2012 15:31:14 -0400 Received: from [192.168.0.200] (5ad4bfa0.bb.sky.com [90.212.191.160]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id 2FD7E3E8001 for ; Sat, 4 Aug 2012 15:31:08 -0400 (EDT) Message-ID: <501D7860.7070406@ajf.me> Date: Sat, 04 Aug 2012 20:30:40 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: PHP Internals Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Should we have some sort of isset()-like operator with default? From: ajf@ajf.me (Andrew Faulds) Hi there, In the "Implict isset in ternary operator" thread, the idea of a special version of the ternary operator with an isset implied was proposed. I don't like this idea very much, but from that I want to suggest some sort of "default" operator. On IRC I had a little discussion about it with nikic (Nikita), salathe (IRC realname is Peter) and Tyrael (Ferenc). Basically, I end up writing code like this a lot: if (!isset($relations[$prev])) { $relations[$prev] = []; } I don't like isset(), it looks quite ugly to me (although I'm fine with empty()). And I think a non-keyword operator for it would be more readable, and save space. My basic idea is summed up by salathe here: https://gist.github.com/3256826 An operator (let's use ? for the time being, whether that becomes the actual operator or not) is proposed that functions like isset, so: $var? // same as isset(var) This on its own is already shorter than isset(), and imo, nicer to read: if ($var?) { /* ... */ } However, I also propose a variant on the "var ?" form, which is "var ? default", for specifying a default: $var ? "default" // same as isset($var) ? $var : "default" This would allow you to express this: if (!isset($_REQUEST['p'])) { $page = 'home'; } else { $page = $_REQUEST['p']; } More concisely as: $page = $_REQUEST['p'] ? 'home'; But I also propose something like a ?= operator, such that you can fill in defaults where a variable doesn't exist: $array['thing'] ?= "default value"; So, to some up: | $var ? => isset($var) $var ? default => isset($var) ? $var : default $var ?= default => $var = $var ? default => $var = isset($var) ? $var : default| Thoughts? This is all hypothetical, of course. It is probably not difficult to implement, but I think it needs discussing before implementing, I am not going to rush. Thanks. -- Andrew Faulds http://ajf.me/