In daily PHP programming, we often need to output strings or numbers in a specific format. At this time, the sprintf() function is your good helper. It not only makes the output more controllable, but also makes the code more neat and readable. This article will take you to systematically master the usage methods and techniques of sprintf() .
sprintf() is a built-in function in PHP that returns formatted strings as results ( no direct output ). This is similar to printf() , but printf() is the direct output, while sprintf() is the result that you can handle freely after returning it.
The function prototype is as follows:
string sprintf(string $format, mixed ...$values)
$format : Format template string.
$values : One or more values to be inserted in the format template.
$name = 'Xiao Ming';
$city = 'Beijing';
$message = sprintf("Hello,I am%s,I am coming from%s。", $name, $city);
echo $message;
Output result:
Hello,I amXiao Ming,I am coming fromBeijing。
$price = 49.5;
$message = sprintf("The price of the product is %.2f Yuan", $price);
echo $message;
Output result:
The price of the product is 49.50 Yuan
illustrate:
%.2f means a floating point number that retains two decimal places.
Format characters | meaning | Example |
---|---|---|
%s | String | sprintf("Hello %s", "Tom") |
%d | Integer (decimal) | sprintf("Age is %d", 25) |
%f | Floating point number | sprintf("Score is %.1f", 98.6) |
%x | hexadecimal | sprintf("number is %x", 255) |
%02d | Integers with less than two digits of zero | sprintf("number is %02d", 5) => number is 05 |
Suppose you need to generate a link to the product details page, with parameters including the product ID and name:
$productId = 123;
$productName = 'Bluetooth headphones';
$url = sprintf("https://gitbox.net/product/view.php?id=%d&name=%s", $productId, urlencode($productName));
echo $url;
Output result:
https://gitbox.net/product/view.php?id=123&name=%E8%93%9D%E7%89%99%E8%80%B3%E6%9C%BA
You can repeat a parameter in the format string using numbering:
$name = 'Zhang San';
$message = sprintf("%1\$s,Hello!%1\$s,Please check the email。", $name);
echo $message;
Output result:
Zhang San,Hello!Zhang San,Please check the email。
Description: %1\$s means the first parameter is used.
$line = sprintf("| %-10s | %10s |", "merchandise", "price");
echo $line;
Output result:
| merchandise | price |
illustrate:
%-10s : left-aligned, accounting for 10 characters wide;
%10s : Right aligned, accounting for 10 characters wide.
Although using sprintf() is flexible in formatting, you should still pay attention to safety when outputting to web pages, databases, and files:
When outputting to a web page, remember to use htmlspecialchars() ;
When building SQL, avoid using sprintf() directly. It is recommended to use parameter binding (such as preprocessing statements for PDO or mysqli);
The parameters in the URL are recommended to be encoded with urlencode() .
sprintf() is a powerful and elegant formatting tool. As long as you master the basic syntax and format control characters, you will be able to add wings to string processing. Whether it is page display, logging, or generating structured data, it can help you write clearer and maintainable code.
Apply it to your project!