SyntaxStudy
Sign Up
Home PHP Reference sprintf()

sprintf()

function

Returns a formatted string. Common specifiers: %s (string), %d (integer), %f (float), %05d (zero-padded).

Syntax

sprintf(string $format, mixed ...$values): string

Example

php
<?php
echo sprintf('Name: %s, Age: %d', 'Alice', 28);
// Name: Alice, Age: 28
echo sprintf('%.2f', 3.14159); // 3.14
echo sprintf('%05d', 42);      // 00042
echo sprintf('Price: $%.2f', 9.99); // Price: $9.99