PHP Print class functions using summary _php Tips

Source: Internet
Author: User
Tags php print sprintf
Copy Code code as follows:

<?php
/*************by garcon1986*********/
The difference between print and Echo:
1. Echo can enter multiple strings, and print cannot.
print "Hello". " World "; Success
The echo "Hello". World "; Success
Print "Hello", "World"; Failed
echo "Hello", "World"; Success
2. echo is faster than print.
$stime = Microtime (true);
print "Hello". " World ";
$etime = Microtime (true);
$total = $etime-$stime;
echo $total. ' <br/> ';
Microtime-return current Unix timestamp with microseconds
$stime 2 = Microtime (true);
The echo "Hello". World ";
$etime 2 = Microtime (true);
$total 2 = $etime 2-$stime 2;
echo $total 2. ' <br/> ';
Execution results:
helloworld0.0014331340789795
helloworld0.00018310546875
See echo faster than print.
Print_r-prints human-readable information about a variable or array
$a = "SAJFD Sfjal sfjalwureoi WEU sj we FK IO";
Print_r ($a);
echo ' <br/> ';
$a = Array ("B", "C", "D");
Print_r ($a);
echo ' <br/> ';
Var_dump-dumps information about a variable or array
Var_dump--Information on printing variables
$a = "SAJFD Sfjal sfjalwureoi WEU sj we FK IO";
Var_dump ($a);
echo ' <br/> ';
$a = Array ("B", "C", "D");
Var_dump ($a);
echo ' <br/> ';
Var_dump (Array ("B", "C", "D"));
echo ' <br/> ';
?>
<?php
/************by garcon1986********/
%%-return percent symbol
%b-Binary number
%c-Characters with ASCII values
%d-Signed decimal digits
%e-Continuous counting method (e.g. 1.5e+3)
%f-floating-point number (Local settings Aware)
%F-floating-point number (not local settings aware)
%o-Eight binary number
%s-String
%u-Unsigned decimal digits
%x-16 Number (small letter)
%x-16 Number (capital letter)
printf () function output formatted string
$str = "Hello";
$number = 456;
Example1
printf ("%s World.") Day number%s ", $str, $number); Output: Hello world. Day Number 456
print "<br/>";
Example2
printf ("percent%", $number); //%
print "<br/>";
printf ("%b", $number); 111001000
print "<br/>";
printf ("%c", $number); ASCII code
print "<br/>";
printf ("%d", $number); 456
print "<br/>";
printf ("%e", $number); 4.560000e+2
print "<br/>";
printf ("%f", $number); 456.000000
print "<br/>";
printf ("%F", $number); 456.000000
print "<br/>";
printf ("%o", $number); 710
print "<br/>";
printf ("%s", $number); 456
print "<br/>";
printf ("%u", $number); 456
print "<br/>";
printf ("%x", $number); 1c8
print "<br/>";
printf ("%x", $number); 1c8
print "<br/>";
printf ("With 2 decimals:%1\$.2f<br/>with no decimals:%1\ $u <br/>", $number);
With 2 decimals:456.00
With no decimals:456
printf ("With 2 decimals:%f<br/>with no decimals:%1\ $u <br/>", $number);
With 2 decimals:456.000000
With no decimals:456
The fprintf () function writes the formatted string to the specified output stream (for example, a file or database).
$file = fopen ("Text.txt", "w");
Echo fprintf ($file, "fprintf 1:%s world. Day number%u ", $STR, $number). ' <br/> '; 38
Echo fprintf ($file, "fprintf 2:%f", $number). ' <br/> '; 21st
Echo fprintf ($file, "fprintf 3:with 2 decimals:%1\$.2f\nwith no decimals:%1\ $u", $number). ' <br/> '; 56
The vprintf () function outputs a formatted string.
The arg parameter in vprintf () is in the array. The percentage of the elements of the array that will be inserted into the main string (%) Symbol Place. The function is executed incrementally. In the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on.
vprintf ("vprintf:%s World.") Day number%u ", Array ($STR, $number)); Vprintf:hello World. Day Number 456
echo ' <br/> ';
The sprintf () function writes the formatted string to a variable.
$txt = sprintf ("sprintf:%s World.") Day number%u ", $STR, $number);
echo $txt. ' <br/> '; Sprintf:hello World. Day Number 456
VFPRINTF () operates as fprintf () but accepts an array of arguments, rather than a variable number of arguments.
Echo vfprintf ($file, "vfprintf:%s world! Day number%u ", Array ($STR, $number)). ' <br/> '; 37
VSPRINTF () operates as sprintf () but accepts an array of arguments, rather than a variable number of arguments.
$txt = vsprintf ("vsprintf:%s World.") Day number%u ", Array ($STR, $number));
echo $txt. ' <br/> '; Vsprintf:hello World. Day Number 456
?>

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.