Analysis of common php string output methods (echo, print, printf and sprintf)

Source: Internet
Author: User
This article mainly introduces common php string output methods, and analyzes the specific usage and usage of echo, print, printf, and sprintf output strings in the form of examples, for more information about php string output, see the following section. For your reference, refer to the following:

1. echo usage: echo can be used for direct output or echo () output without returning values.

$ String ="Bold display text"; Echo $ string; // echo"
"; // Echo ($ string); // The effect is the same as echo"
"; Echo" This "," is "," echo test! "; // Echo multiple strings separated by commas (,). echo"
";

2. print usage: same as echo, print is slower than echo, and only one string can be output at a time, always returning 1

$ String ="Bold display text"; Print $ string; // print"
"; // Print ($ string); // The effect is the same as print"
";

3. printf: format the output string

/* %-Return a percent sign % B-binary number % c-ASCII value corresponding to the character % d-contains the plus and minus digits in decimal number (negative, 0, positive) % e-use lower-case scientific notation (e.g. 1.2e + 2) % E-use upper-case scientific notation (e.g. 1.2E + 2) % u-decimal number (greater than or equal to 0) % f-floating point number (local) % F-floating point number (non-local) % g-shorter % e and % f % G-shorter % E and % f % o-octal digits % s-string % x-hexadecimal digits (lowercase letters) % X-hexadecimal number (uppercase letters) */$ num = 23; printf ("printf output floating point: % f", $ num); // output: 23.000000 printf ("
"); Printf (" printf outputs two decimal floating point: % 1 \ $. 2f
Printf output has no decimal places: % 1 \ $ u ", $ num); // output: 23.00 printf ("
");

4. sprintf:

$ Name = "Tom"; $ age = 20; $ printstr = sprintf ("sprintf output: His name is % s, and his age is % u", $ name, $ age ); echo $ printstr; // output: he is named Tom and his age is 20.

I hope this article will help you with php programming.

For more articles on common php string output methods (echo, print, printf, and sprintf), please follow the PHP Chinese network!

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.