Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102297 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37210 invoked from network); 18 Jun 2018 15:32:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jun 2018 15:32:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.26 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.26 out2-smtp.messagingengine.com Received: from [66.111.4.26] ([66.111.4.26:40379] helo=out2-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/95-29356-A90D72B5 for ; Mon, 18 Jun 2018 11:32:42 -0400 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 09C4421D69 for ; Mon, 18 Jun 2018 11:32:39 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Mon, 18 Jun 2018 11:32:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=CatKPp4v/fIYi4s6eF2zU3WFFR/LB JyFNdnTPvvI+XE=; b=CryxWO5rhGUeJc8U82LbQFjRKPrM/jp+3IDr/BROUKJ3u 5Lzd/360wlIqS5c2DaZVVEIh3+v8k5M7aYuzhPwvxiUECE6JP1OoZ9bM6d2AyLYn n8Lmb4sbnxniLphRY+lVlBOuTGW4aWaj1PtRKvUp9pRc5N+bgxkYaVhgVIvYt9J1 4uVNPshnd2xMmQ2ratM+PdtzpkqLbC//y3oUEYBQfXszJtDQzQiqPvEdSf9XD3i/ jyOZFjfP461o4JZYmr7KhWqVShwHVsBlEZ/aJiB17SC/fgRLLPGPExtb1nkdU97G zEDZhnhRCel9X/hlLcDjw9KBs7cEGcEJiZ3RkDDxA== X-ME-Proxy: X-ME-Sender: Received: from vulcan.localnet (216-80-30-152.s3222.c3-0.frg-cbr1.chi-frg.il.cable.rcncustomer.com [216.80.30.152]) by mail.messagingengine.com (Postfix) with ESMTPA id B69711026E for ; Mon, 18 Jun 2018 11:32:38 -0400 (EDT) To: internals@lists.php.net Date: Mon, 18 Jun 2018 10:32:38 -0500 Message-ID: <3242366.BL9MUqXe46@vulcan> In-Reply-To: <73ac8914-8c9f-e74b-a4f5-f6e39cd96f8f@gmx.de> References: <75db877c-ef0b-7d3b-48a8-9879d90352b0@web.de> <76e4744f-6722-ee49-0325-e4433236f1b1@web.de> <73ac8914-8c9f-e74b-a4f5-f6e39cd96f8f@gmx.de> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart14162766.9Mr9zVhB2m"; micalg="pgp-sha512"; protocol="application/pgp-signature" Subject: Re: [PHP-DEV] [RFC][Under Discussion] Add functions array_key_first()and array_key_last() From: larry@garfieldtech.com (Larry Garfield) --nextPart14162766.9Mr9zVhB2m Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" On Monday, June 18, 2018 4:25:27 AM CDT Christoph M. Becker wrote: > On 18.06.2018 at 00:09, Enno Woortmann wrote: > > In my opinion that's the argument to return null for either parameters > > which aren't an array or empty arrays simply because a first/last key > > isn't present and null is the value to be returned for an undefined state. > > I've tested this behavior with other array_* functions which are > > provided in the PHP core and they provide null consistently, eg.: > > > > root@WOL-Soft-DEVVM:/var/www/WOL-Soft# php -r "var_dump(array_keys(1));" > > PHP Warning: array_keys() expects parameter 1 to be array, integer > > given in Command line code on line 1 > > NULL > > root@WOL-Soft-DEVVM:/var/www/WOL-Soft# php -r > > "var_dump(array_values('hello'));" > > PHP Warning: array_values() expects parameter 1 to be array, string > > given in Command line code on line 1 > > NULL > > root@WOL-Soft-DEVVM:/var/www/WOL-Soft# php -r > > "var_dump(array_flip(null));" > > PHP Warning: array_flip() expects parameter 1 to be array, null given > > in Command line code on line 1 > > NULL > > These return values are due to zend_parse_parameters() failures (i.e. > the given types don't match the expected types), and it's customary to > return NULL in this case (under strict typing an exception would be > thrown instead). However, most functions signal other failures by > returning FALSE, for instance: > > var_dump(array_search('foo', [])); // => bool(false) > > I would prefer, if these other functions would return NULL, though, but > we can't change that for BC reasons. Returning false for "not found" rather than "null" is a big middle-finger to users. :-) Besides the reasons here, the violation of type constraints totally screws up the surrounding code. Especially with null-coalesce. For instance, this thing of beauty: $secret = getenv('APP_SECRET') ?? $_SERVER['APP_SECRET'] ?? getenv('DEFAULT_SECRET') ; doesn't actually work, because getenv() returns false instead of null. Instead you have to do this mess: $secret = (getenv('APP_SECRET') ?: null) ?? $_SERVER['APP_SECRET'] ?? (getenv('DEFAULT_SECRET') ?: null) ; See also: https://www.garfieldtech.com/blog/empty-return-values Can we please never return false for error/not-found, ever, period, forever more? It's a fundamentally broken idiom that needs to die. --Larry Garfield --nextPart14162766.9Mr9zVhB2m Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE/ph/GXfY8v0YwFBp4MMKwDFxYXcFAlsn0JYACgkQ4MMKwDFx YXcWZwgAk93LpkqEPkgfzGDRYHYr4Pq5snPYGFVHeSpcAb35LR1fMTjyAA0w2QJA wJItn41PUxNi9875Sp5+UdP1G25imj1pf/QUKc+Y14IGzYI2mFUF5UUj3DVome7i oi/cg15gMjD/Wjlhr4l1iy46vYW+BZwWf9alN4P94oQatGEWZiWg0LsynpgBxPyL ypjAh3lr0P/AhbwZD5dXflRHtEBdMmsPVp6wNVmXEJ8A5WoqlC8d3PSy2oSleGPe sZATwbfc6fD3cgPg+QVWDc3ML//1GnmKy2f+XWzDse66Iih/rjYAfzaizP82qxlb wJnU2JDkkpe7dM0OUoISpulFzMGxzQ== =YYE/ -----END PGP SIGNATURE----- --nextPart14162766.9Mr9zVhB2m--