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.
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.
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.
As a silly example, I prefer this:
$sentence = "The {$adjectives[0]} {$adjectives[1]} {$nouns[0]} jumped
over the {$adjectives[2]} {$nouns[1]}";
To this:
$sentence = sprintf(
'The %s %s %s jumped over the %s %s',
$adjectives[0],
$adjectives[1],
$nouns[0],
$adjectives[2],
$nouns[1]
);
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.
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.
I won't be too upset if this feature doesn't get added, but I do think
it would be a nice addition.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php