Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101608 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70474 invoked from network); 17 Jan 2018 18:18:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jan 2018 18:18:01 -0000 Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.50 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:43041] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/6F-12394-6539F5A5 for ; Wed, 17 Jan 2018 13:17:59 -0500 Received: by mail-wm0-f50.google.com with SMTP id g1so16978812wmg.2 for ; Wed, 17 Jan 2018 10:17:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=luL0TRc0FpagLKmMR5/OivT07gu/tgSnhfe1XCYhVHs=; b=AOo0Rnh/n2YdVGPK1rB0nlzKjE/ZAGVKV/JEOAxSI+qnnAjU2axuwTZKH4ugNg23jY /FDXZzpHCvLwF8FriATv+zXs6H1QNGtvWozhqcnajg0jZZuqTMrDVkuF93VRtW9nyDAd cE3EA1ipO0sedEOdnWQwpJMFtAagH/Fe5Yl0tS/aq2ESamaKKeZIRk0CJZsxAdbmdjx2 H5t+q1DFXlFat+DUzfh6lSYfNgvPR5bvgyp0wNV4Mi/qv0Tasi3V0kKgrGnQnOQf3d++ 3CRbpdUC+YSeOaz6XL/KK51zPRIBZeAxSrPrv8SVmPYHC4s/VtBpSLqFwJI8dEMutegT b54Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=luL0TRc0FpagLKmMR5/OivT07gu/tgSnhfe1XCYhVHs=; b=oZifRJ0EwOvexMfJ2vaXyblVV9oip7LPSb0SaAby3kavwHLjaIl/bApMk9KfF0SBUF R6npTwGysmAzvbN0YLKPv4lQA3QXSLT8Dsh3a/270I/kZDRP+4H5DftXPU/sDYFuQ5Jl dfDJhQruPI0HfCTNY4d2+GpoyzqtYACtOh6sd8QnR34z2j9ovO4COI/NQ/UwbpCo3sNC ZksPWnreVO+Upgz8xZDZxbX9hd7pAIgzsI2FL4DK/ZUlPoQkeA/R05YV20/qnh9XYDEl VCMP0buVf8gN8kt+wVcKw2kIOf3ieSIwAukMuQ94dOBVEpd5SMT0fKYdGqce3/A0QXA+ 7DxA== X-Gm-Message-State: AKwxytc8E/15C0CZdJw5fTh+EHpvOyO+IWHiQxAPZkTRfiVFJJcsAY4A S4tMA+CmsElEhpuILGkdjS0QbLaaI7p5Zqj+RNkfPQ== X-Google-Smtp-Source: ACJfBovs3Dx2ysFOxc3wxwR91qF+fCUMtZTGtJXyPuZrMVqvY/0alTA2KjXGR3OQjvHE0isx0TEcRyhItBXzKZEPqq0= X-Received: by 10.28.170.195 with SMTP id t186mr3039062wme.3.1516213075711; Wed, 17 Jan 2018 10:17:55 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.153.161 with HTTP; Wed, 17 Jan 2018 10:17:54 -0800 (PST) Received: by 10.223.153.161 with HTTP; Wed, 17 Jan 2018 10:17:54 -0800 (PST) In-Reply-To: <6fac0482-93fe-b15f-632c-1058232abcf0@eordes.com> References: <6fac0482-93fe-b15f-632c-1058232abcf0@eordes.com> Date: Wed, 17 Jan 2018 19:17:54 +0100 Message-ID: To: Lito Cc: PHP Internals List Content-Type: multipart/alternative; boundary="001a114432c2c81cea0562fcdd9f" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:) From: ocramius@gmail.com (Marco Pivetta) --001a114432c2c81cea0562fcdd9f Content-Type: text/plain; charset="UTF-8" This: echo (isset($foo) && $foo) ? $foo : 'default'; Is equivalent to: echo $foo ?: 'default'; Please don't endorse usage of undefined variables. On 17 Jan 2018 19:00, "Lito" wrote: > Related with Request #75833 https://bugs.php.net/bug.php?id=75833 > > From PHP 7 null-coalescing operator is a great option to avoid a previous > exists check with isset. > > But I think that this comaparison can be improved adding a ternary > operator like ??: and check also empty values. > > Example: > > -------------------------------------- > > > $foo = ''; > > // Current response with ternary operator > > echo $foo ?: 'default'; // 'defaut' > > // Current response with null-coalescing operator > > echo $foo ?? 'default'; // '' > > // Possible response with ternary null-coalescing operator > > echo $foo ??: 'default'; // 'defaut' > > // Same response with current PHP available code > > echo (isset($foo) && $foo) ? $foo : 'default'; // 'default' > > /** ------------------------------- **/ > > $foo = false; > > // Current response with ternary operator > > echo $foo ?: 'default'; // 'defaut' > > // Current response with null-coalescing operator > > echo $foo ?? 'default'; // false > > // Possible response with ternary null-coalescing operator > > echo $foo ??: 'default'; // 'defaut' > > // Same response with current PHP available code > > echo (isset($foo) && $foo) ? $foo : 'default'; // 'default' > > /** ------------------------------- **/ > > $foo = null; > > // Current response with ternary operator > > echo $foo ?: 'default'; // 'defaut' > > // Current response with null-coalescing operator > > echo $foo ?? 'default'; // 'defaut' > > // Possible response with ternary null-coalescing operator > > echo $foo ??: 'default'; // 'defaut' > > // Same response with current PHP available code > > echo (isset($foo) && $foo) ? $foo : 'default'; // 'default' > > /** ------------------------------- **/ > > unset($foo); > > // Current response with ternary operator > > echo $foo ?: 'default'; // PHP Notice: Undefined variable: foo > > // Current response with null-coalescing operator > > echo $foo ?? 'default'; // 'defaut' > > // Possible response with ternary null-coalescing operator > > echo $foo ??: 'default'; // 'defaut' > > // Same response with current PHP available code > > echo (isset($foo) && $foo) ? $foo : 'default'; // 'default' > > -------------------------------------- > > What about? > > Thanks! > Lito. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a114432c2c81cea0562fcdd9f--