Re: [RFC][Under discussion] Arbitrary string interpolation

From: Date: Fri, 25 Mar 2022 15:43:33 +0000
Subject: Re: [RFC][Under discussion] Arbitrary string interpolation
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-117427@lists.php.net to get a copy of this message
Hey Ilija. On 25.03.22 15:38, Arnaud Le Blanc wrote:
Hi Ilija I find that sprintf() is easier to read in most cases. One reason for this is that the text is separated from the code. It's also easier to review for humans and linters. The strtoupper example would look like this with sprintf:
     $world = 'world';
     echo sprintf('Hello %s!', strtoupper($world));
Longer examples can be nicely split in multiple lines:
     echo sprintf(
         'Received HTTP status code %d (reason phrase: %s)',
         $response->getStatusCode(),
         $response->getReasonPhrase(),
     );
This technique also allows me to use the original string in translation while still keeping the replacements out of that. So something like
    echo sprintf(
        gettext('Hello %s!'),
        strtoupper($world)
    );
would easily work and provide the translator only with the string `Hello %s which is much less to break than Hello {strtoupper($world)}!` So for internationalized applications I'd rather keep the sprintf approach than a new string-syntax. A way to use sprintf in a more out-of-the-box way like in ruby would be awesome! Something like
    echo 'Hello %s' % [strtoupper($world)];
or
    echo 'Hello %{world}' % ['world' => strtoupper($world)];
But I'm absolutely happy with the current sprintf functionality. Just my 0.02€ Cheers Andreas -- ,,, (o o) +---------------------------------------------------------ooO-(_)-Ooo-+ | Andreas Heigl | | mailto:andreas@heigl.org N 50°22'59.5" E 08°23'58" | | https://andreas.heigl.org | +---------------------------------------------------------------------+ | https://hei.gl/appointmentwithandreas | +---------------------------------------------------------------------+

Attachment: [application/pgp-keys] OpenPGP public key OpenPGP_0xA8D5437ECE724FE5.asc
Attachment: [application/pgp-signature] OpenPGP digital signature OpenPGP_signature.asc
« previous php.internals (#117427) next »