PHP output function Print, printf, sprintf the difference

Source: Internet
Author: User
Tags sprintf

the commonly used string output in PHP is: use the Echo and print two methods to display the string. You can also try the printf and sprintf functions if the format output operation is involved.

First, say Echo and print two ways

    1. The syntax format for print is:int print ( string $arg )
    2. Echo's syntax format is:void echo ( string $arg1 [, string $... ])

Echo is a function-like output, not a function, no return value, and supports multiple parameters;

Print is an output function whose return value is 1 of type int, and only one parameter is supported.

echo usage can be comma "," to connect multiple parameters, you can also use the English dot number "." As a hyphen, make up a parameter, and give a chestnut:

Echo CHR (ten); Echo ' This '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n";

Print cannot use the comma "," to connect multiple parameters, only the English dot number "." form a parameter as a hyphen. For example, the second sentence above is written in print:

Print (' this '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n");

second, the focus has come, in PHP , both the sprintf and printf functions are used to format the operation strings, and the output syntax formats are:

  1. The syntax format for
  2. printf is: int printf     (string $format    [, mixed $args    [, mixed $ ...  ])
  3. sprintf syntax format is:string sprintf ( string $format [, mixed $args [, mixed $... ]] )

The difference between the two is:

    1. The printf () function can output the formatted string directly, and the sprintf () function needs to use the Echo method to output the formatted string.
    2. The return value of the printf () function is of type int, which represents the number of characters printed out of the string, and the return value of the sprintf () function is a string.

For example, we all understand:

echo $str. "     <br> "; The output here is a example for this.
$number =print ($str. "    <br> "); The output here is a example for this.
echo $number. "  <br> "; Output here 1

$num 1=printf ($format, 65,65,65,65); Here output 1000001, A, 65, 65
echo "<br>";
echo $num 1. "  <br> "; Here output 18, corresponding to the number of strings
Echo sprintf ($format, 97,97,97,97); Using echo Output 1100001, A, 97, 97

PHP output function Print, printf, sprintf the difference

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