Sprintf function usage in php _ PHP Tutorial

Source: Internet
Author: User
Sprintf function usage in php. Previously, 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 have read some materials. I have heard from my teacher before that, before using the echo method to display and output formatted strings, I will 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:

$ 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:

Differences between echo, print, printf, and sprintf in PHP
$ Str = 'Let/'s study php! '';
Echo $ str ."
";
$ Number = print $ str ."
";
Echo $ number ."
";
$ Format = "% B % c % d ";
$ Number1 = printf ($ format, 88,88, 88 );
Echo"
". $ Number1;
Echo"
". 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.

The explain () function can display and output formatted strings only by using the echo method. let's take a look at the simple method below. I am reading some documents, books...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.