PHPprint function usage Summary

Source: Internet
Author: User
Tags php print
PHPprint class function summary, allowing you to thoroughly understand the specific usage and scope of use of the print function.

PHP print function summary, allowing you to thoroughly understand the specific usage and scope of use of the print function.

The Code is as follows:
/************ By garcon1986 *********/
// Difference between print and echo:
// 1. echo can input multiple strings, but print cannot.
Print "hello". "world"; // succeeded
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 .'
';
// Microtime-Return current Unix timestamp with microseconds
$ Stime2 = microtime (true );
Echo "hello". "world ";
$ Etime2 = microtime (true );
$ Total2 = $ etime2-$ stime2;
Echo $ total2 .'
';
// Execution result:
// Helloworld0.0014331340789795
// Helloworld0.00018310546875
// The echo is 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 ($ );
Echo'
';
$ A = array ("B", "c", "d ");
Print_r ($ );
Echo'
';
// Var_dump-Dumps information about a variable or Array
// Var_dump -- print information about the variable
$ A = "sajfd sfjal sfjalwureoi weu sj we fk io ";
Var_dump ($ );
Echo'
';
$ A = array ("B", "c", "d ");
Var_dump ($ );
Echo'
';
Var_dump (array ("B", "c", "d "));
Echo'
';
?>
/************ By garcon1986 ********/
// %-Return percentage sign
// % B-binary number
// % C-ASCII characters
// % D-Signed decimal number
// % E-resumable counting (such as 1.5e + 3)
// % F-floating point number (local settings aware)
// % F-floating point number (not local settings aware)
// % O-Octal numbers
// % S-string
// % U-Unsigned decimal number
// % X-hexadecimal (lowercase letter)
// % X-hexadecimal (uppercase letters)
// The printf () function outputs formatted strings.
$ Str = "hello ";
$ Number = 456;
// Example1
Printf ("% s world. Day number % s", $ str, $ number); // output: hello world. Day number 456
Print"
";
// Example2
Printf ("%", $ number); // %
Print"
";
Printf ("% B", $ number); // 111001000.
Print"
";
Printf ("% c", $ number); // ascii code
Print"
";
Printf ("% d", $ number); // 456.
Print"
";
Printf ("% e", $ number); // 4.5620.e + 2
Print"
";
Printf ("% f", $ number); // 456.000000.
Print"
";
Printf ("% F", $ number); // 456.000000.
Print"
";
Printf ("% o", $ number); // 710.
Print"
";
Printf ("% s", $ number); // 456.
Print"
";
Printf ("% u", $ number); // 456.
Print"
";
Printf ("% x", $ number); // 1c8
Print"
";
Printf ("% X", $ number); // 1C8
Print"
";
Printf ("With 2 decimals: % 1 \ $. 2f
With no decimals: % 1 \ $ u
", $ Number );
// With 2 decimals: 456.00
// With no decimals: 456
Printf ("With 2 decimals: % f
With no decimals: % 1 \ $ u
", $ Number );
// With 2 decimals: 456.000000
// With no decimals: 456
// The fprintf () function writes formatted strings 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 ).'
'; // 38
Echo fprintf ($ file, "fprintf 2: % f", $ number ).'
'; // 21
Echo fprintf ($ file, "fprintf 3: With 2 decimals: % 1 \ $. 2f \ nWith no decimals: % 1 \ $ u", $ number ).'
'; // 56
// The vprintf () function outputs formatted strings.
// The arg parameter in vprintf () is located in the array. The element of the array is inserted into the percentage (%) symbol of the Main string. This function is executed step by step. 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'
';
// The sprintf () function writes formatted strings into a variable.
$ Txt = sprintf ("sprintf: % s world. Day number % u", $ str, $ number );
Echo $ txt .'
'; // 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 )).'
'; // 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 .'
'; // 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.