PHP base echo, print, print_rvar_dump Difference Comparison

Source: Internet
Author: User
Tags parse error php basics sprintf

PHP Basics tutorial: (basic) Comparison between echo, print, print_r, printf, sprintf, var_dump

first, Echo

Echo () is not actually a function, it is a PHP statement, so you do not need to use parentheses on it. however, if you want to pass more than one argument to echo (), A parse error occurs when you use Parentheses. and echo returns void and does not return a value, so it cannot be used to assign a Value.

Example:

650) this.width=650; "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00T1313.png "alt=" Training "style=" border:0px; "/>

second, Print

Print () is the same as Echo (), but the echo speed is a little bit faster than print. It is not actually a function, so you do not need to use parentheses on it. however, if you want to pass more than one parameter to print (), A parse error occurs when you use Parentheses. Note that print always returns 1, which is different from echo, which means you can use print to assign a value, but it doesn't make sense.

Example:

650) this.width=650; "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00T95K.png "alt=" Training "style=" border:0px; "/>

three, Print_r function

The Print_r function prints easy-to-understand information about Variables.

Syntax: mixed Print_r (mixed $expression [, bool Return])

If the variable is a string, the integer or float will output its value directly, and if the variable is an array, it will output a formatted array for readability, which is the format of the key and Value. Similar for object Objects. Print_r has two parameters, the first is a variable, the second can be set to true, and if set to true, returns a string, otherwise returns a Boolean value of True.

Example:

650) this.width=650; "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00UO95.png "alt=" Training "style=" border:0px; "/>

four, printf function

The printf function returns a formatted STRING.

Syntax: printf (format,arg1,arg2,arg++)

The parameters format is the converted format, starting with the percent sign ("%") to the end of the converted Character. The following is the possible format value:

*%%– return percent symbol

*%b– binary number

*%c– characters in accordance with ASCII values

*%d– Signed decimal number

*%e– Counting Method (E.G. 1.5e+3)

*%u– unsigned decimal number

*%f– floating point (local settings Aware)

*%f– floating point number (not Local Settings Aware)

*%o– octal number

*%s– string

*%x– Hexadecimal number (lowercase Letter)

*%x– Hexadecimal number (capital Letter)

arg1, arg2, arg++, etc. parameters will be inserted into the main string percent percent (%) Symbol. The function is executed incrementally, in the first% symbol, insert arg1, Insert arg2 at the second% symbol, and so On. If the% symbol is more than the ARG parameter, you must use a placeholder. After the placeholder is inserted in the% symbol, It consists of a number and a "\$". You can use numbers to specify the parameters that are displayed, see examples for Details.

Example:

650) this.width=650, "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00ZNS.png "alt=" in-train "style=" border : 0px; "/>

five, sprintf function

This function uses the same method as printf, except that the function writes the formatted string to a variable instead of the Output.

Example:

650) this.width=650; "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q0091K94.png "alt=" Training "style=" border:0px; "/>

vi.. var_dump function

Function: the content, type, and length of the output Variable's contents, type, or String. Commonly used to Debug.

650) this.width=650; "src=" http://www.cdtedu.com/uploads/allimg/161018/1-16101Q00924635.png "alt=" Training "style=" border:0px; "/>


PHP base echo, print, print_rvar_dump Difference Comparison

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.