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!