In the past, I heard from my teacher that the sprintf () function must use the echo method to display the formatted string and output. Let's take a look at the simple method.
I am reading some documents and always see the following code when reading books:
The Code is as follows: |
Copy code |
$ Query = sprintf ('insert INTO % sUSER (USERNAME, PASSWORD ,'. 'Email _ ADDR, IS_ACTIVE, PERMISSION) VALUES ("% s", % d, % d )', DB_TBL_PREFIX, Mysql_real_escape_string ($ this-> username, $ GLOBALS ['db']), Mysql_real_escape_string ($ this-> password, $ GLOBALS ['db']), Mysql_real_escape_string ($ this-> emailAddr, $ GLOBALS ['db']), $ This-> isActive, $ This-> permission ); |
Sprintf is used to format the string. Is there any difference between this writing method and the writing method directly connected with a string connector. Or more
Rigorous
The syntax format of the printf () and sprintf () functions is:
Int printf (string $ format [, mixed $ arg1 [, mixed args2. ..])
String sprintf (string $ format [, mixed $ arg1 [, mixed args2. ..])
A small example:
The Code is as follows: |
Copy code |
<H3> Differences between echo, print, printf, and sprintf in PHP <? PHP $ Str = 'let/'s study php! ''; Echo $ str. "<br/> "; $ Number = print $ str. "<br/> "; Echo $ number. "<br/> "; $ Format = "% B % c % d "; $ Number1 = printf ($ format, 88,88, 88 ); Echo "<br/>". $ number1; Echo "<br/>". sprintf ($ format, 88,88, 88 ); ?> |
Introduction to echo, print, printf, and sprintf
The returned value in print output mode is int type, and the returned value is always 1. The syntax format of print is as follows:
Int print (string $ str)
The echo output method does not return values. The syntax format of echo is as follows:
Void echo (string $ str [, string $ str1. ..])
In PHP, echo and print can be used in general cases, but in some cases, the two methods are also different, as shown in:
1) echo supports multiple parameters, but print only supports one parameter.
2) the return value of echo is void, and that of print is always 1.