Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

PHP Features

Last updated: 09 November 2026