PHP Common String Output method analysis (echo,print,printf and sprintf) [Original]_php Tips

Source: Internet
Author: User
Tags lowercase sprintf

This article describes the PHP common string output method. Share to all of you for reference, specific as follows:

1. Echo Usage: Can be directly output with ECHO, or echo () output, no return value

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

2. Print usage: ditto echo, only print runs slower than Echo, and only one string at a time, always returns 1

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

3. printf: Format output string

* Percent
%-returns a percent semicolon%
%b-binary number
%c-ascii the character
%d-contains positive and negative decimal numbers (negative, 0, positive)
%e-Scientific notation using lowercase (for example, 1.2e+2)
%E-Use the scientific notation of uppercase (for example, 1.2E+2)
%u-a decimal number (greater than or equal to 0) that does not contain a positive sign
%f-floating-point numbers (Local settings)
%f-floating-point numbers (not local settings)
%g-Shorter%E and%f
%G-Shorter%E and%f
%o-Eight number of
%s-string
%x-16 (lowercase)
%x-16 (capital Letter)
*/
$num =23;
printf ("printf output floating-point number:%f", $num);//output: 23.000000
printf ("<br/>");
printf ("printf Output 2 decimal floating-point number:%1\$.2f <br/>printf output No decimal places:%1\ $u", $num);/output: 23.00
printf ("<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, age is 20

I hope this article will help you with the PHP program design.

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.