Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117476 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93640 invoked from network); 5 Apr 2022 00:28:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Apr 2022 00:28:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0AC8F1804D0 for ; Mon, 4 Apr 2022 18:57:53 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 4 Apr 2022 18:57:52 -0700 (PDT) Received: by mail-oo1-f44.google.com with SMTP id v19-20020a056820101300b0032488bb70f5so2042879oor.5 for ; Mon, 04 Apr 2022 18:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20210112.gappssmtp.com; s=20210112; h=from:mime-version:subject:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=vlqngmJscJLZ5Q0H1bc5dQIADYCYHAroHbaCtRUysFY=; b=2bWddyrritzfYn1t8koiQwaQSOLyBv/aeP3t80AHFe/W2n7M5hyfdweGwgEjf/1XXc oTTZBV4AMd5RFeZIT4vPps6GOhtQUudFXpkpScTR7EF2q0mfCVF9uQY3e6LDjDkBKrxG 5/ouMYG6PvBQJvLRVYens0vTq0RM2hCxWlW2bTGKY/Y8rE/D6636XH3Z3OiR3S0Z/Ksy +rwf+0nLGeua7JxrVm5NmHOlRecxkdrDrhisnhQo5Rwou8v6+/yKedeotmMOveDUW+RV ZpEDy2jffZ1vUMFNOVtRhnNAfBwyUDcVINShmG46CJ7ei/6+UHEMLEmSGemtDI/wcPMc X+2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:subject:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=vlqngmJscJLZ5Q0H1bc5dQIADYCYHAroHbaCtRUysFY=; b=rdAL8AICMUuvEAbxc2PT9SnXe+iu/+huhlk/8wL2OoulFYrmtJX13XesVW7n+hPrRf jsMI/v6Xv2KUTjqY7mDSq5vnmgMqzrbUIgfr2zrp1tXq10zIvd1TgxGXpwujSw/M1H58 5RBXJ6W/mQdRWtJcB6zmetaEt985oY9xK2de+6rrnoIpu17F1XmcXuZWDRNmtcIvx982 mKjWggDNI80PRY1FpepnqhAjmAVJDdtHVasOyqK5JwYBhki4XTw5ZkQq0+qwwZLdYqS9 1hqfraLnrzCNG0abJYZG5mi7LyruIhZZYPVpjjNDVyn74NOWVj353chTsld+mE4vmx88 YmMw== X-Gm-Message-State: AOAM5309fJvhur7Doy9og1dEmqpF1GTYrbHfy/NDhXCV6vh9Fv7fN9ta ZuIRWE5QUtWrIY5oSLh9N4U51Q== X-Google-Smtp-Source: ABdhPJyiC16vE43K0vShwxBWL4RP/WvOVfgUcaKcX8DCYhwavG9+F9p0jFtoEhAY2y9zCoretTt51w== X-Received: by 2002:a4a:c4cc:0:b0:324:725b:5028 with SMTP id g12-20020a4ac4cc000000b00324725b5028mr353724ooq.14.1649123871825; Mon, 04 Apr 2022 18:57:51 -0700 (PDT) Received: from [15.111.204.238] (ext-104-36-248-13.arubanetworks.com. [104.36.248.13]) by smtp.gmail.com with ESMTPSA id e7-20020a9d2a87000000b005af548abe73sm5362317otb.4.2022.04.04.18.57.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Apr 2022 18:57:51 -0700 (PDT) X-Google-Original-From: MKS Archive Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) In-Reply-To: Date: Mon, 4 Apr 2022 21:57:49 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <3477D049-A9D3-49C5-9578-DAA2D8C26121@gmail.com> References: <10558027.nUPlyArG6x@arnaud-t490> To: Rowan Tommins X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Re: [PHP-DEV] [RFC][Under discussion] Arbitrary string interpolation From: mike@newclarity.net (MKS Archive) > On Mar 26, 2022, at 6:15 PM, Rowan Tommins = wrote: >=20 > On 25/03/2022 14:38, Arnaud Le Blanc wrote: >> I find that sprintf() is easier to read in most cases. One reason for = this is >> that the text is separated from the code. >=20 >=20 > Funnily enough, I find sprintf() *harder* to read for the same reason = - particularly once there are more than two or three parameters, and = more than a bit of punctuation between them. >=20 > A large part of that is because the placeholders are positional rather = than named, so you have to keep track of which is which; but by the time = you've got named placeholders, you might as well have variable = interpolation. >=20 > As a silly example, I prefer this: >=20 > $sentence =3D "The {$adjectives[0]} {$adjectives[1]} {$nouns[0]} = jumped over the {$adjectives[2]} {$nouns[1]}"; >=20 > To this: >=20 > $sentence =3D sprintf( > 'The %s %s %s jumped over the %s %s', > $adjectives[0], > $adjectives[1], > $nouns[0], > $adjectives[2], > $nouns[1] > ); >=20 > I think that's partly a matter of taste, though, because I've = definitely seen people happily using both styles. And there are = certainly situations (like translation strings) where placeholders of = some sort work better than straight interpolation. Choice of sprintf() vs string interpolation *can* be more than a matter = of taste, and this from someone who once greater preferred the latter. There are (at least) two use-cases I am familiar with where using = printf() and sprintf() with placeholders have benefits over string = interpolation. 1.) Internationalization of human-readable strings can be more easily = facilitated when the literal text is kept distinct from the interpolated = values. 2.) Reduced memory allocation and string manipulation when strings are = used for logging that may be discarded when logging levels are set to = less than "log everything."=20 #fwiw > That's why I thought it was interesting to see what other languages = have done. While PHP and Ruby have obvious links back to Perl, many = languages which didn't start off with string interpolation have added it = in later versions, e.g. C#, Scala, JavaScript, Python. Clearly there = were sufficient voices in favour in each of those communities to add it; = and in each case, they added *expression* interpolation, not just the = *variable* interpolation supported by Perl and PHP. >=20 > I won't be too upset if this feature doesn't get added, but I do think = it would be a nice addition. >=20 > Regards, >=20 > --=20 > Rowan Tommins > [IMSoP] >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20