PHP Common String Output method analysis (echo,print,printf and sprintf)

Source: Internet
Author: User
This article describes the PHP common string output method. Share to everyone for reference, specifically as follows:

1. Echo Usage: Use echo to output directly or echo () output, no return value

$string = "<b> bold display text </b>", Echo $string;//echo "<br/>";//echo ($string);//Effect echo "<br/>"; echo "This", "is", "Echo test!"; /echo outputs multiple strings separated by commas echo "<br/>";

2. Print usage: Same as echo, just print runs slower than Echo and can only output one string at a time, always returning 1

$string = "<b> bold display text </b>";p rint $string;//print "<br/>";//print ($string);//Effect ditto print "<br/> ";

3. printf: Formatting the output string

/*%%-Returns a percent sign%%b-the binary number%C-ASCII value corresponding to the character%d-contains the signed decimal number (negative, 0, positive)%e-use lowercase scientific notation (for example 1.2e+2)%e-use uppercase scientific notation (e.g. 1.2E+2)%u- Decimal number without sign (greater than or equal to 0)%f-floating-point number (local setting)%f-floating point (non-local setting)%g-shorter%e and%f%g-shorter%e and%f%o-eight decimal number%s-string%x-16 decimal (lowercase)%x-16 in System number (capital letter) */$num =23;printf ("printf output floating point:%f", $num);//output: 23.000000printf ("<br/>");p rintf ("printf Output 2-bit decimal floating-point number:%1 \$.2f <br/>printf output No decimal digits:%1\ $u ", $num);//output: 23.00printf (" <br/> ");

4. sprintf:

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

I hope this article is helpful to you in PHP programming.

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.