Comparison of echo, print, print_r, printf, sprintf, and var_dump output by php _ PHP Tutorial

Source: Internet
Author: User
Comparison of echo, print, print_r, printf, sprintf, and var_dump output by php. I have been using. net for five years, and recently I want to get started with it. Net, so let's take a look at php. In learning php, first look at several output functions. I. echoecho () has been developed with. net for five years. Recently, I suddenly wanted to contact you. Net, so let's take a look at php. In learning php, first look at several output functions.
1. echo
Echo () is actually not a function, it is a php statement, so you do not need to use parentheses for it. However, if you want to pass more than one parameter to echo (), a parsing error occurs when brackets are used. Besides, echo returns void and does not return values. Therefore, it cannot be used to assign values.
Example:

The code is as follows:


$ A = echo ("55nav"); // error! Cannot be used for value assignment
Echo "55nav"; // 55nav
Echo ("55nav"); // 55nav
Echo ("55nav", "com"); // An error occurred. multiple parameters cannot be passed in parentheses.
Echo "55nav", "com", "is", "web"; // when no parentheses are needed, multiple values can be separated by commas, and 55nav com is web
Echo "55nav is 8 good 9 web."; // no matter whether it is a line feed or not, the final display is a line of 55nav is good web.
$ Fistname = "55nav ";
Echo "$ fistname com"; // if $ firstname = "55nav", 55nav com is output.
Echo '$ firstname com'; // because single quotes are used, the value of $ firstname is not output, but $ firstname com is output.
?>


II. print
Print () is the same as echo (), but the echo speed is a little faster than print. In fact, it is not a function, so you do not need to use parentheses for it. However, if you want to pass more than one parameter to print (), a parsing error occurs when brackets are used. Note that print always returns 1, which is different from echo, that is, print can be used to assign values, but it has no practical significance.
Example:

The code is as follows:


$ A = print ("55nav"); // This is allowed
Echo $ a; // The value of $ a is 1.
?>


III. print_r functions
The print_r function prints easy-to-understand information about variables.
Syntax: mixed print_r (mixed $ expression [, bool return])
If the variable is string, integer or float, its value is directly output. if the variable is an array, a formatted array is output for ease of reading, that is, the format corresponding to the key and value. Object objects are similar. Print_r has two parameters. The first parameter is a variable, and the second parameter can be set to true. if it is set to true, a string is returned. Otherwise, a Boolean value is returned.
Example:

The code is as follows:


$ A = "55nav ";
$ C = print_r ($ );
Echo $ c; // The value of $ c is TRUE.
$ C = print_r ($ a, true );
Echo $ c; // The value of $ c is the string 55nav.
?>


IV. printf functions
The printf function returns a formatted string.
Syntax: printf (format, arg1, arg2, arg ++)
The format parameter is the conversion format. it starts with the percent sign ("%") and ends with the conversion character. The following are possible format values:
* %-Percentage sign returned
* % B-binary number
* % C-ASCII characters
* % D-signed decimal number
* % E-resumable counting (for example, 1.5e + 3)
* % U-unsigned decimal number
* % F-floating point number (local settings aware)
* % F-floating point number (not local settings aware)
* % O-octal values
* % S-string
* % X-hexadecimal (lowercase letter)
* % X-hexadecimal number (uppercase letters)
Parameters such as arg1, arg2, arg ++ are inserted to the percent sign (%) in 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. If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "\ $. You can use numbers to specify the displayed parameters. For more information, see the example.
Example:

The code is as follows:


Printf ("My name is % s. "," 55nav "," com "); // My name is 55nav com.
Printf ("My name is % 1 \ $ s % 1 \ $ s", "55nav", "com "); // add 1 \ $ or 2 \ $ ..... this line outputs My name is 55nav 55nav because only the first parameter is displayed twice.
Printf ("My name is % 2 \ $ s % 1 \ $ s", "55nav", "com"); // My name is com 55nav
?>


5. sprintf functions
This function is used in the same way as printf. The only difference is that the function writes formatted strings to a variable instead of output.
Example:

The code is as follows:


Sprintf ("My name is % 1 \ $ s % 1 \ $ s", "55nav", "com"); // you will find nothing to output.
$ Out = sprintf ("My name is % 1 \ $ s % 2 \ $ s", "55nav", "com ");
Echo $ out; // output My name is 55nav com
?>


VI. var_dump function
Function: outputs the content, type, and length of the variable, type, or string. It is often used for debugging.

The code is as follows:


$ A = 100;
Var_dump ($ a); // int (100)
$ A = 100.356;
Var_dump ($ a); // float (100.356)
?>


Bytes. Net, so let's take a look at php. In learning php, first look at several output functions. 1. echo () actual...

Related Article

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.