Home  >  Article  >  Backend Development  >  Detailed usage of sprintf function in php_PHP tutorial

Detailed usage of sprintf function in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:011102browse

I heard from the teacher before that the sprintf() function needs to use the echo method to display the formatted string output. Let me take a look at the simple method.

I always see code like the following when reading some information and books

A small example:
The code is as follows
 代码如下 复制代码

$query = sprintf('INSERT INTO %sUSER (USERNAME, PASSWORD, ' .
               'EMAIL_ADDR, IS_ACTIVE, PERMISSION) VALUES ("%s", "%s", "%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);

Copy code

$query = sprintf('INSERT INTO %sUSER (USERNAME, PASSWORD, ' .

                'EMAIL_ADDR, IS_ACTIVE, PERMISSION) VALUES ("%s", "%s", "%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);
 代码如下 复制代码

PHP中echo,print,printf,sprintf的区别


$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);
?>


Sprintf is used here to format the string. Is there any difference between this way of writing and the way of directly connecting them with string connectors? In other words, this is better

Be more rigorous

The syntax format of printf() function and sprintf() function is:
int printf(string $format[,mixed $arg1[,mixed args2...]])
String sprintf(string $format[,mixed $arg1[,mixed args2...]])

The code is as follows
Copy code

The difference between echo, print, printf and sprintf in PHP

Introduction to echo, print, printf, sprintf
The return value of print output mode is of type int, and the return value is always 1. The syntax format of print is as follows: int print(string $str) The echo output method has no return value. The syntax format of echo is as follows: void echo(string $str[,string $str1...]) In PHP, echo and print are generally interchangeable, but in some cases there are differences between the two methods, as shown in: 1) echo supports multiple parameters, but print only supports one parameter 2) The return value of echo is void, and print has a return value that is always 1 http://www.bkjia.com/PHPjc/628935.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628935.htmlTechArticleI heard from the teacher before that the sprintf() function needs to use the echo method to display the formatted string output. , let me look at the simple method below. I am reading some information, books...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn