Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113515 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 48107 invoked from network); 14 Mar 2021 15:59:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Mar 2021 15:59:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 207FC1804D8 for ; Sun, 14 Mar 2021 08:53:03 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12olkn2069.outbound.protection.outlook.com [40.92.23.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 14 Mar 2021 08:53:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JUt842UaQ2tdi8YNzRfW0uews8E+Q6vEuH8VnHW4jc6+hUMuELejoC+CZDpxGg7zxBoAe1nljBkXQUSMzrR0hx2J4iqIWNmRmCH2/ctqw9yKVI7f0fm/4v/eV+a3WKbyFUxhKjxpIWfOhsbyXuYIaFPMwW857V3LQMN/gGPBcJAzc7nv/cwhuBzKfUTqoLu1Vt48mNZrZGkelZMotlDGTxrHlam9fNsXiztkNZG1sxSfotdQzA2wr6F113aOMHYolzPQu4S7zt802Oa/1EeRMoO+ZK2Mx86lxY/TogJJ0+Fsptz4v79T55jGCOKhlbp35+4PKgMb/NdTPP5wWaOMbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SX+7xpFqZVIIxl7yhjnhLN8fMZjXIF9g6K0UuQmr8/A=; b=L+9LARTI886Fr1fxDPzlGyDks2HXP7Wft831lUyVtkINlwqie2JTUXxGxwVzveu602EmwlFIdsb0Fg2+nRTF6lgVb15ZF7JQ4YBxvbyiV+Acdt0ek/Cj8XzD5I+Z5Mo7K71gXH72Q69sHLJ39n9S7ImIEz9KK+rn3neGdnvGUuPeuWsaZdgHqOdMQ6FoHxLqYdK3yUt0c46M2G+jaG7ecAJrFJRlLrC8xb8YMVc6WGW3S6CEXYZuje37DtnQD7wchK8rYmby0wOaVvc4Wtw9SF1X8ewpMqlIUhIgBhUZ2H9saOp8KUmHJ8A+ZhswiH0aXRwWv/XF+28yVJ47b8rOOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SX+7xpFqZVIIxl7yhjnhLN8fMZjXIF9g6K0UuQmr8/A=; b=g4fCv7nVF5jvG4OBZJLfECCp2h8YkKENRbBOuEp03SmtTQTm2zJLEafPGc5hQAxiKM92TVBnZFdZn2cxlYPBNS9ElO++MO7CDa5r54WDO1+yZSWyCjB6twS6umnWVdGbcd9mSl26EkYZJpTM9bNefIOIfDoslPbEcdbrEMYhs0UEUqSL067BPXDra2Uxj3n9adK9u9jrgAZOQQFJ2g2XIsjM2rkWWLEuZSs0FEHOaVw+lrqtoDrzD1y/xSlH43oMBagjm6tiq354NBVPz8ib9FI4E0+uysVjokZKvocYHIhhmotuLmkjuqaAJrUymSaXG/cxrJpHExjZfB7kaB6H2A== Received: from BN8NAM12FT064.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc66::46) by BN8NAM12HT050.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc66::386) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.9; Sun, 14 Mar 2021 15:53:00 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:fc66::53) by BN8NAM12FT064.mail.protection.outlook.com (2a01:111:e400:fc66::118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.9 via Frontend Transport; Sun, 14 Mar 2021 15:53:00 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::9c7c:2273:6416:6a0b]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::9c7c:2273:6416:6a0b%6]) with mapi id 15.20.3933.031; Sun, 14 Mar 2021 15:53:00 +0000 To: "internals@lists.php.net" Thread-Topic: [PHP-DEV] RFC: Add `println(string $data = ''): int` Thread-Index: AQHXGD2+ZEBcHEKLP06OksoLbxBVZ6qDhsuAgAAP43M= Date: Sun, 14 Mar 2021 15:52:59 +0000 Message-ID: References: , In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:1FB1CE02C637D4892B415CCE87F0C34F8261C746DF6AB5F692FF6F02EBEF633D;UpperCasedChecksum:3F8F3022D2D4CE661EEEEC925E9ECD80A3273A4FC57C2E32842C1BCE9A9892DC;SizeAsReceived:7089;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [xtVofGk4/ofspu8ODfQRS/P9q4OrGWKjYssjeXGRTLPJf0Ugq1gN+1uSfe4RXs+X] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 4f09d5bd-e4ac-4347-57f4-08d8e7013e5d x-ms-traffictypediagnostic: BN8NAM12HT050: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: VYH9btBOsomkuzWqIZZ4VN9jWvMKm/rBoa8bVr3CfRjpI4IdrxNl6H4bVBcHqQqUyzXw8DOZXpEe+YLfH4HAZaGTghrhoB5Poxw+suLNgAjZ4LngW1/54tsCmSLVrxebbSq0XpBAebYEoxst1ArVBXlOoCT+ZuJwsnP2D1uUZSCXPldb86X12piKfssOTj3oHhi+ZFdhgnd6v+wUeDiP4LArf571aubK9zei9PYLwJ+Rxjyu/qb2hqTqEyUCCXUgXs0wXkKCpUqpXnJVwBTlRBcEzwiRHP/zyPBdl4zI37VBhNynlOZ/rgIVMw+AVp7FLi1aCsvi4OWDkKMe25etYBDSLZ7wYwxt+5Og49idTXESLJYPjIVmEy01PS2UbrY9QXtR5OQ7YFHzEUc5Y2UhoIdVlf7yQtdCEPTxvzFLYdYjIzoqUMZDPyazXO++Z0wE x-ms-exchange-antispam-messagedata: nWcUTYB86iJZ4xNN5HqkbgEdlvqHoedp2FD2yTsBC7jWNFhALi+Hwk657s4xTkwDapUb9isWl5QE64EWgGxfkGBzq53ZAYq++16CF0qGMpUXtronx0e9i23gzrXUHh2YxfpptiIKCU7EkZhW1Y46XBVD5vtl6eLZQ2an9W4z0AXVk5PEA+7jK0NVYUqbEONMnCoj1XzxBvZM4feQCkCupg== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: BN8NAM12FT064.eop-nam12.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 4f09d5bd-e4ac-4347-57f4-08d8e7013e5d X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Mar 2021 15:52:59.9304 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8NAM12HT050 Subject: Re: [PHP-DEV] RFC: Add `println(string $data = ''): int` From: tysonandre775@hotmail.com (tyson andre) Hi Rowan,=0A= =0A= > Hi Tyson,=0A= > =0A= > I'm on the fence on this one: I've certainly had occasions when it would = =0A= > be useful, but mostly in quick prototypes and demo code, and just as =0A= > often in HTML context (where I'd want it to add '
') as plain text =0A= > context.=0A= > =0A= > I am not keen, however, on the proposed implementation details, and =0A= > would prefer it to much more closely match either print or echo.=0A= > =0A= > =0A= > - Until reading this RFC, I would not have guessed that printf() =0A= > returned the number of bytes written; I guess it's useful in C, where =0A= > you're managing string buffers, but in PHP it feels very arbitrary. I =0A= > would also not particularly associate this new function with printf() so = =0A= > my immediate guess would be the same return value as print (even though = =0A= > that return value, int(1), is equally arbitrary).=0A= =0A= There's also the alias fputs of https://www.php.net/manual/en/function.fwri= te.php=0A= which also returns the byte count.=0A= =0A= I'd considered just returning void, but then you'd have a mix of standard l= ibrary=0A= functions that did/didn't return void and users would need to remember whic= h did what.=0A= If someone actually had a need to use the result of println, it would likel= y be to count bytes,=0A= rather than to get a hardcoded constant such as 1/null.=0A= (or expression chaining, but `println('Starting') or die(2); // somehow fai= led to output` also has a use case)=0A= =0A= > - You explicitly state that this function would depend on strict_types = =0A= > mode, but echo and print do not. I can't see any particular advantage to = =0A= > doing so, and 'println((string)$foo)' would be longer than 'print =0A= > $foo,"\n"' rather defeating the purpose.=0A= =0A= I could change it to accept `mixed` and convert it to a string inside of th= e function.=0A= (or `object|string|float|int|bool|null` and throw or return false if an obj= ect could not be converted to a string).=0A= Throwing/warning for bool/null in strict mode may be worth it but may be mo= re convenient to detect with external static analyzers)=0A= It initially seemed consistent with the behavior of https://www.php.net/fwr= ite to only accept a string=0A= and forcing callers to do so would catch unexpected edge cases with bool/nu= ll (''/'1'),=0A= floats, etc. in strict mode.=0A= =0A= Surprisingly, some output functions in PHP do accept `mixed`.=0A= https://www.php.net/file_put_contents accepts `mixed` and casts non-resourc= es =0A= and non-arrays to strings (php-src/ext/standard/file.c)=0A= (handling of arrays/resources is different)=0A= =0A= ```=0A= case IS_NULL:=0A= case IS_LONG:=0A= case IS_DOUBLE:=0A= case IS_FALSE:=0A= case IS_TRUE:=0A= convert_to_string(data);=0A= =0A= case IS_STRING:=0A= ```=0A= =0A= Annoyingly, any decision I make would be inconsistent with something,=0A= e.g. file_put_contents currently doesn't even emit a notice for a failure t= o convert to string.=0A= =0A= ```=0A= php > var_dump(file_put_contents('test.txt', new stdClass()));=0A= bool(false)=0A= ```=0A= =0A= It definitely would be longer to use `println((string)$foo)`,=0A= but there may be cases where that would be done,=0A= e.g. if you are writing a script that would be reviewed/modified/used by = =0A= programmers that are more familiar with languages that aren't PHP.=0A= (It'd be more readable using 1 output method than 3)=0A= =0A= There's also `println("$foo");`=0A= =0A= > - Most importantly, I accept your points about a function being more =0A= > forward- and backward-compatible, but worry that not making it a keyword = =0A= > will lead to further confusion about how parentheses interact with echo = =0A= > and print. There is a common misconception that they have some kind of = =0A= > "optional parentheses", because they are usually used with a single =0A= > expression, so wrapping it in parentheses usually doesn't change the =0A= > outcome; but this is not the case, and it does sometimes matter.=0A= > =0A= > =0A= > As currently proposed, I can see people getting nasty surprises from =0A= > these inconsistencies. For instance:=0A= > =0A= > print (1+2)*3; // prints "9"=0A= > println (1+2)*3; // prints "3\n"; unless strict_types=3D1 is in effect, i= n =0A= > which case TypeError=0A= > print ($foo ?? 'Foo') . ($bar ?? 'Bar'); // prints "FooBar" if both vars = =0A= > are null=0A= > println ($foo ?? 'Foo') . ($bar ?? 'Bar'); // never prints $bar or =0A= > "Bar", because they are not passed to println()=0A= =0A= Static analyzers for PHP such as Phan warn about the unused result of multi= plication=0A= to catch cases like this, and other analyzers can easily add this check if = they don't already check for it.=0A= https://github.com/phan/phan/wiki/Issue-Types-Caught-by-Phan#phannoopbinary= operator=0A= =0A= > if ( something() && println($foo) && somethingElse() )=A0 // does what it= =0A= > looks like, if println is a normal function=0A= > if ( something() && print($foo) && somethingElse() )=A0 // does not print= =0A= > $foo, because the expression passed is actually ($foo)&&somethingElse()= =0A= =0A= The fact that `print` doesn't require parenthesis is something that surpris= ed me initially,=0A= though changing it to force it to use function syntax would be a much large= r bc break=0A= more suitable for a major version, that I don't expect to pass.=0A= It may be possible to start deprecating `print` if the expression didn't co= ntain a parenthesis=0A= (like the change in precedence of `+` and `-` did), but I'm not planning to= work on that.=0A= (Python converted `print` from a statement in Python 2 to a call in Python = 3)=0A= =0A= Regards,=0A= Tyson=0A=