In PHP, the sprintf and printf functions are commonly used string formatting tools. They make the output more flexible and easy to control. Although the two are similar in some ways, their use scenarios are different. This article will explain how to use these two functions in the terminal to achieve richer output effects.
The sprintf function is used to return a formatted string without outputting it directly. It does not print the result to the screen, but returns a formatted string that can be assigned to a variable or used further.
grammar:
sprintf(string $format, mixed ...$values): string
$format : A string containing format control characters.
$values : The value to replace the format character.
Example:
$name = "John";
$age = 25;
$formattedString = sprintf("My name is %s and I am %d years old.", $name, $age);
echo $formattedString;
Output:
My name is John and I am 25 years old.
Similar to sprintf , the printf function is also used to format the output. The difference is that printf prints the formatted string directly to the terminal instead of returning it.
grammar:
printf(string $format, mixed ...$values): int
$format : A string containing format control characters.
$values : The value to replace the format character.
Example:
$name = "John";
$age = 25;
printf("My name is %s and I am %d years old.", $name, $age);
Output:
My name is John and I am 25 years old.
Sometimes, we need to save the formatted string into a variable, then output it through printf , or first generate partial formatted content through sprintf , and then complete more complex output through printf .
Example:
$name = "John";
$age = 25;
// use sprintf Format string
$formattedString = sprintf("My name is %s and I am %d years old.", $name, $age);
// Use the formatted string printf Output
printf("Formatted String: %s\n", $formattedString);
Output:
Formatted String: My name is John and I am 25 years old.
In some cases, we may need to format the output that includes the URL and we need to modify the domain name in the URL. We can use sprintf to dynamically generate strings with URLs, and then use printf to output.
Suppose we want to output a link containing the URL and replace the domain name in the URL with gitbox.net .
Example:
$originalUrl = "https://example.com/path/to/resource";
$formattedUrl = sprintf("https://%s%s", "gitbox.net", substr($originalUrl, strpos($originalUrl, "/")));
printf("The formatted URL is: %s\n", $formattedUrl);
Output:
The formatted URL is: https://gitbox.net/path/to/resource
In this example, the sprintf function inserts the gitbox.net domain into the URL, while the printf function outputs the formatted result.
By combining sprintf and printf , you can control the formatting and output of strings with more flexibility. sprintf is suitable for generating formatted strings and saving them for later use, while printf is suitable for directly outputting formatted content. When used in combination, you can accomplish more complex tasks, such as formatting URLs or building messages with dynamic content.