printf() Vs echo
<?php
declare(strict_types=1);
class x {
function __toString() {
return 'abc';
}
}
$a = new x;
$b = 1.23;
printf('%s %d', $a, $b);
echo PHP_EOL;
echo "$a ". (int) $b;
An upcoming optimisation in PHP 8.6 is the conversion from printf() call to echo. %s only convert the argument to string, and then, %d cast it to integer. When there are no other operation, such as formatting, then the echo call with concatenations and cast is actually faster. PHP 8.6 optimize it on the fly for you.
In previous version, that is a micro optimisation, so unless you call this a few million times, keep it and upgrade to PHP 8.6.
See Also
- printf versus echo [Try me]
PHP Features
Last updated: 09 November 2026