Hướng dẫn sprintf vs printf php
I written the following tiny Show
the output is this:
why I have that 6 digit in the first line of output?
asked Nov 25, 2014 at 7:10
1 printf doesnt return the string, it directly outputs it (and returns only its length). Try this
Output
Fiddle Now you might ask why that extra 6 (in your output) then? Becuase printf returns the length of the printed string which is 6 in your case. So here is how it goes
answered Nov 25, 2014 at 7:13
Hanky PankyHanky Panky 46.2k8 gold badges70 silver badges95 bronze badges printf :- directly print the formatted string. sprintf :- convert given format and store values in a variable and you can use echo/print to print variable values.
answered Nov 25, 2014 at 7:18
Rakesh SharmaRakesh Sharma 13.6k4 gold badges36 silver badges42 bronze badges Not the answer you're looking for? Browse other questions tagged php or ask your own question.The Sample Code below shows the difference between printf vs sprintf Difference
'; // Declare few Variable $name="Brian88"; $country =" Malaysia"; $age=37; //Declare the main Statement $statement = " Hi my name is %s, I am from %s , I am %d years old"; // Save the String Variable to an Output $output = sprintf($statement,$name,$country,$age); // echo output echo $output; ?> Tác dụng của hàm printf()The This function takes a special format string and then any number of other arguments, which will be formatted and spliced into the specified places in the format string to generate the result. Related functions: The following table summarizes the technical details of this function.
SyntaxThe basic syntax of the printf(format, arg1, arg2, argN); The following example shows the
Note: Every character in the format string will be printed literally, except the % character and characters that immediately follow it. The % character indicates the beginning of a conversion specification, which specifies how to print the arguments that follow the format string. Tip: The ParametersThe
Conversion Specification SyntaxThis section describes the syntax of conversion specification in the format string in detail. A conversion specification begins with a percent symbol (%). You must include a conversion specification for each argument that is passed to the Alternatively, you can use the argument number followed by a dollar sign (i.e. A conversion specification typically has the following syntax:
argnumAn integer followed by a dollar sign $, to specify which number argument to treat in the conversion. flagsThe flags consist of one or more of the following characters:
widthAn integer that indicates how many characters (minimum) this conversion should result in. precisionA period (.) followed by an integer who's meaning depends on the specifier:
If the period is specified without an explicit value for precision, 0 is assumed. specifierA single character indicating how the type of the argument should be interpreted.
More ExamplesHere're some more examples showing how The following example shows how to apply multiple conversion specifications to the same argument.
Tip: If the format string is enclosed in double-quotes (""), you need to escape the dollar sign after argnum with a backslash character (\), like this The following example shows how to format the same number with and without decimal points.
By default, each conversion specification will be replaced by the formatted argument in the order they are listed inside the function. But, you can swap arguments inside the format string using argnum.
Tip: If the order of the placeholders or conversion specifications in the format string does not match the order of the arguments listed inside the function. You can use argnum to easily indicate which arguments the placeholders refer to and leave the function code as is. The following example demonstrates the use of various format specifiers for different types of data.
The following example shows how to format a string in different ways using s specifier.
Bài viết này đã giúp ích cho bạn? |