Why use sprintf function in PHP?
sprintf
has all the formatting capabilities of the original printf which means you can do much more than just inserting variable values in strings.
For instance, specify number format (hex, decimal, octal), number of decimals, padding and more. Google for printf and you'll find plenty of examples. The wikipedia article on printf should get you started.
Printf and sprintf where exactly used in php? give one example?
Using sprintf have many options to format your output string. This SO answer will help you more:
Why use sprintf function in PHP?
Performance of variable expansion vs. sprintf in PHP
In all cases the second won't be faster, since you are supplying a double-quoted string, which have to be parsed for variables as well. If you are going for micro-optimization, the proper way is:
$message = sprintf('The request %s has %d errors', $request, $n);
Still, I believe the seconds is slower (as @Pekka pointed the difference actually do not matter), because of the overhead of a function call, parsing string, converting values, etc. But please, note, the 2 lines of code are not equivalent, since in the second case $n is converted to integer. if $n is "no error" then the first line will output:
The request $request has no error errors
While the second one will output:
The request $request has 0 errors
sprintf php - How does it work?
Sprintf
basix
Sprintf
adds the parameters to the first parameter at the place of the placeholders (which begins with %
).
For instance:
$name = 'Rob';
$str = sprintf('Hello, I am %s', $name); // become: Hello, I am Rob
The letter after %
is the first letter of the type of the parameter. A string is %s
, an decimal is %d
. For instance:
$name = 'Rob';
$age = 26;
$str = sprintf('Hello, I am %s and I am %d years old.', $name, $age);
// become: Hello, I am Rob and I am 26 years old.
Sprintf
use the order of the parameters to determine where to place it in the string. The first placeholder gets the second parameter, the second placeholder the thirth, ect.
If you want to change this, you need to specify this between %
and the type. You do this by <place>$
where <place>
is the place nummer.
$name = 'Rob';
$age = 26;
$str = sprintf('Hello, I am %2$s and I am %1$d years old.', $age, $name);
// become: Hello, I am Rob and I am 26 years old.
The answer
You do this:
'<h1><a href="%1$s">%s</a></h1><p>%s</p>'
The %1$s
is the first parameter, which is the template_uri
. I think this isn't the url which you want to link to? You want to link to the uri in $link
. Just place that in the parameters and refer to it:
sprintf(
'...'.
'<h1><a href="%s">%s</a></h1><p>%s</p>',
Website::get('template_uri'), $icon, $link, $title, $text
);
Explanation of this format string used in PHP sprintf
%
is the start of the conversion specification01\$
signifies that the value will be placed in the first item of the resultMore usually, the same would be written as
1$
.2
is the precision specifier, which dictates how many decimal digits should be displayedf
means that the argument is treated as a float, and presented as a floating-point number (locale aware)
For full details of the above, and what is available, see the description of the format
parameter of sprintf()
.
Why is sprintf giving asterisks instead of my formatted string?
This issue was actually caused because the sequence of numbers matched the Luhn Algorithm, and was being masked in my PCI environment. I manually excluded the file from the test, and everything now is working as expected.
PHP, Printf,Sprintf Functions
The sign specifier forces a sign, even if it's positive. So, if you have
$x = 10;
$y = -10;
printf("%+d", $x);
printf("%+d", $y);
You'll get:
+10
-10
The padding specifier adds left padding so that the output always takes a set number of spaces, which allows you to align a stack of numbers, useful when generating reports with totals, etc.
$x = 1;
$y = 10;
$z = 100;
printf("%3d\n", $x);
printf("%3d\n", $y);
printf("%3d\n", $z);
You'll get:
1
10
100
If you prefix the padding specifier with a zero, the strings will be zero padded instead of space padded:
$x = 1;
$y = 10;
$z = 100;
printf("%03d\n", $x);
printf("%03d\n", $y);
printf("%03d\n", $z);
Gives:
001
010
100
Related Topics
How to Access Magento User's Session from Outside Magento
Oauth Implementation in Netsuite Using PHP
How to Pass Value from One PHP Page to Another Using Session
Phpmailer - the Following Smtp Error: Data Not Accepted
How to Include a PHP.Ini File in Another PHP.Ini File
Laravel Model Callbacks After Save, Before Save, etc
PHP Include Causes White Space at the Top of the Page
PHP String Interpolation Syntax
Pdo SQLite Could Not Find Driver... PHP File Not Processing
Array to Object and Object to Array in PHP - Interesting Behaviour
Adding a Namespace When Using Simplexmlelement
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
How to Refresh a Page When a Database Is Updated
Validating Us Phone Number with PHP/Regex
Does the Order of Class Definition Matter in PHP
Mysqli + Xdebug Breakpoint After Closing Statement Result in Many Warnings