Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101609 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72074 invoked from network); 17 Jan 2018 18:28:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Jan 2018 18:28:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=info@eordes.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=info@eordes.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain eordes.com from 209.85.128.177 cause and error) X-PHP-List-Original-Sender: info@eordes.com X-Host-Fingerprint: 209.85.128.177 mail-wr0-f177.google.com Received: from [209.85.128.177] ([209.85.128.177:33608] helo=mail-wr0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/BF-12394-3C59F5A5 for ; Wed, 17 Jan 2018 13:28:20 -0500 Received: by mail-wr0-f177.google.com with SMTP id p6so19962944wrd.0 for ; Wed, 17 Jan 2018 10:28:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eordes-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=jIAEA/LQAMnpNCue161wmdCZ4t/JcNSX6M//sczp1QI=; b=MrppC4++mPq76bV7QgmV6IxpsbEZbULGJ+hBQLAv+s5IhOv4fGh4ayMdGhlMEZNTTk RctF6D8wbj+wMry5M1DlukztgdbyBRh00XsIQRzka9Mv6qptt5EnbhQopXDcmpTvqHIF 5Zc5Jp15CoS0F4Z8z38pR4HWVcQFcbQUqdMtVn8khi4uX1fU6JkNlC9bdWWutXv9Gexi 4KmF4dqCmzatJs3kUyri8JWJEDz+YhCRjDcJMxcm/U15aldtyBbHayl0CmnIEAFhaHfg 70/227XkmOHlo6BQjMS3jweGlaUY7oAqPcchu5pGfUMIr4dLGmYdfVI6L3cWoEneNLw1 fOxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=jIAEA/LQAMnpNCue161wmdCZ4t/JcNSX6M//sczp1QI=; b=Wbrja9qjCyl84Aa0YX6fbC07Za2b9fU3wgJ1HH2hBJmjI3a/ucx50bV1zxAN6oJgJc Ek8eVdnROJtkKCDuBvf3vKVi/qWGfGa/oUGHK8MQS4ARlRtyjvuNE8n5H9TwepGloimy WRn0Uet5VG3Bak2/FPCGfKzn97yZ1oIsSFJNqh8Ye9wX/DnOH8pBvUDqeEkHhjxUubq+ y3GeWr5FqDUZjGMgSLNBV3y6AfwDFhQ30jy3wL1fowRDj/8q338Q54SjTs3p7IW8NWY+ 0YZ6OMehNYxzuSZX8g6Z7pFEAoIS5xyQVbkVVi6Uj96tbAuJxntLNQ+UsDUu4xZS5IzV rsvQ== X-Gm-Message-State: AKwxytcgyuRAWz/gmqroeBVUUFCtIaxlA1w/uoWSicM23nOl5wkPK0mt aSJQkaCqrSz7uBDEtPkIX4l32jAw X-Google-Smtp-Source: ACJfBotobLeCYBufiQuEakVAkO6UZ+/bZ5bstUjfng7nZy8v32lHVfKBw86kfcVDNab7p66iZrFxlw== X-Received: by 10.223.141.235 with SMTP id o98mr3370870wrb.39.1516213696389; Wed, 17 Jan 2018 10:28:16 -0800 (PST) Received: from [192.168.0.20] (static-144-248-27-46.ipcom.comunitel.net. [46.27.248.144]) by smtp.gmail.com with ESMTPSA id w191sm1265185wmw.4.2018.01.17.10.28.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Jan 2018 10:28:15 -0800 (PST) To: Marco Pivetta Cc: PHP Internals List References: <6fac0482-93fe-b15f-632c-1058232abcf0@eordes.com> Message-ID: Date: Wed, 17 Jan 2018 19:28:14 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------B6B0C5A0F4AE95EDF00349F9" Content-Language: es-CO Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Improve null-coalescing operator (??) adding empty check (??:) From: info@eordes.com (Lito) --------------B6B0C5A0F4AE95EDF00349F9 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit No $foo ?: 'default' , it's only equivalent to (isset($foo) && $foo) ? $foo : 'default' if $foo exists. Also PHP has added ?? as null-coalescing operator that works with undefined variables/attributes/keys, my proposal is an improvement over this one. I don't want to endorse usage of undefined variables, can be used in a large set of situations, like object attributes, array keys, etc... Anyway thanks for your feedback. Lito. On 17/01/18 19:17, Marco Pivetta wrote: > 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 > --------------B6B0C5A0F4AE95EDF00349F9--