@ 2025-05-02T06:29:09Z <?php
declare(strict_types=1);
class FooStringable implements \Stringable
{
public function __toString(): string
{
return 'foo';
}
}
function my_printf(mixed ...$args): void {
$printArg = fn (mixed $arg) => match (true) {
is_bool($arg) => $arg ? 'true' : 'false',
is_string($arg) => "'{$arg}'",
is_numeric($arg) => $arg,
is_null($arg) => 'null',
is_object($arg) => $arg::class,
default => var_export($arg, true),
};
$argsTxt = array_map($printArg, $args);
$argsTxt = implode(', ', $argsTxt);
echo "printf({$argsTxt});\n";
try {
printf(...$args);
} catch (Throwable $e) {
echo $e->getMessage() . "\n";
}
echo "\n-------------------------------\n";
};
$stream = fopen('php://stdout', 'w');
// Error
my_printf('%d', new FooStringable());
my_printf('%d', rand() ? new FooStringable() : 5);
my_printf('%f', new FooStringable());
my_printf('%*s', '5', 'a');
my_printf('%*s', 5.0, 'a');
my_printf('%*s', new \SimpleXMLElement('<a>7</a>'), 'a');
my_printf('%*s', null, 'a');
my_printf('%*s', true, 'a');
my_printf('%.*s', '5', 'a');
my_printf('%2$s %3$.*s', '1', 5, 'a'); // * is the first ordinary placeholder, so it matches '1'
my_printf('%1$-\'X10.2f', new FooStringable());
my_printf('%s %1$*.*f', new FooStringable(), 5, 2);
my_printf('%3$f', 1, 2, new FooStringable());
// Strict error
my_printf('%d', 1.23);
my_printf('%d', rand() ? 1.23 : 1);
my_printf('%d', 'a');
my_printf('%d', '1.23');
my_printf('%d', null);
my_printf('%d', true);
my_printf('%d', new \SimpleXMLElement('<a>aaa</a>'));
my_printf('%f', 'a');
my_printf('%f', null);
my_printf('%f', true);
my_printf('%f', new \SimpleXMLElement('<a>aaa</a>'));
my_printf('%s', null);
my_printf('%s', true);
// Error, but already reported by CallToFunctionParametersRule
my_printf('%d', new \stdClass());
my_printf('%s', []);
// Error, but already reported by my_printfParametersRule
my_printf('%s');
my_printf('%s', 1, 2);
// OK
my_printf('%s', 'a');
my_printf('%s', new FooStringable());
my_printf('%d', 1);
my_printf('%f', 1);
my_printf('%f', 1.1);
my_printf('%*s', 5, 'a');
my_printf('%2$*s', 5, 'a');
my_printf('%s %2$*s', 'a', 5, 'a');
my_printf('%1$-+\'X10.2f', 5);
my_printf('%1$*.*f %s %2$d', 5, 6, new FooStringable()); // 5.000000 foo 6
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.6 0.036 0.012 18.22 8.4.5 0.036 0.010 17.96 8.4.4 0.022 0.007 17.75 8.4.3 0.040 0.009 17.66 8.4.2 0.037 0.010 17.78 8.4.1 0.039 0.012 17.91 8.3.20 0.039 0.007 17.08 8.3.19 0.040 0.009 16.85 8.3.18 0.040 0.007 16.45 8.3.17 0.033 0.013 16.73 8.3.16 0.042 0.005 16.84 8.3.15 0.053 0.007 17.11 8.3.14 0.035 0.007 16.92 8.3.13 0.012 0.007 16.79 8.3.12 0.032 0.007 16.84 8.3.11 0.035 0.007 17.14 8.3.10 0.040 0.008 16.79 8.3.9 0.040 0.008 16.74 8.3.8 0.037 0.010 16.67 8.3.7 0.036 0.011 16.61 8.3.6 0.042 0.006 16.82 8.3.5 0.034 0.008 16.76 8.3.4 0.033 0.004 17.81 8.3.3 0.036 0.007 17.93 8.3.2 0.031 0.012 17.77 8.3.1 0.032 0.009 17.68 8.3.0 0.032 0.009 17.65 8.2.28 0.027 0.013 16.93 8.2.27 0.030 0.011 16.78 8.2.26 0.030 0.010 17.15 8.2.25 0.029 0.003 17.01 8.2.24 0.022 0.011 16.79 8.2.23 0.032 0.010 16.75 8.2.22 0.026 0.004 17.00 8.2.21 0.019 0.007 16.81 8.2.20 0.016 0.004 16.77 8.2.19 0.013 0.005 16.94 8.2.18 0.019 0.004 17.16 8.2.17 0.027 0.010 17.81 8.2.16 0.033 0.008 18.17 8.2.15 0.022 0.004 17.82 8.2.14 0.031 0.004 18.06 8.2.13 0.032 0.011 17.84 8.2.12 0.032 0.011 17.97 8.2.11 0.031 0.012 18.00 8.2.10 0.033 0.010 17.89 8.2.9 0.037 0.007 17.85 8.2.8 0.036 0.008 17.57 8.2.7 0.033 0.008 17.88 8.2.6 0.035 0.010 17.79 8.2.5 0.035 0.008 17.77 8.2.4 0.035 0.011 17.93 8.2.3 0.034 0.006 17.55 8.2.2 0.028 0.010 17.98 8.2.1 0.028 0.009 17.88 8.2.0 0.027 0.007 17.72
preferences:dark mode live preview ace vim emacs key bindings
34.38 ms | 403 KiB | 5 Q