PHP output statement echo, print, Print_r, printf, sprintf, var_dump comparison

Source: Internet
Author: User
Tags parse error

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:

<?php $a = Echo ("Xshell"); Error! Cannot be used to assign the echo "Xshell"; Xshell  Echo ("Xshell");//Xshell  Echo ("Xshell", "net");//error occurred, parentheses cannot pass multiple parameters  echo "Xshell", "net", "is", " Web ";  You can separate multiple values with commas without parentheses, and output Xshell NET is Web  echo "Xshell is  good  web.";  The final display is a line Xshell is good web, whether or not it wraps.  echo "$fistname net"; If $firstname = "Xshell", Xshell net is output.  Echo ' $firstname net '; Because of the use of single quotation marks, the $firstname value is not output, but the output $firstname net?>

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:

<?php$a = Print ("Xshell"); This is the allowed Echo $a; The value of the $a is 1?>

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:

<?php $a = "Xshell"; $c = Print_r ($a);  echo $c;  The value of $c is true  $c = Print_r ($a, ture);  Echo $c; The value of the $c is the string xshell?>

The

Four, printf functions
    printf function returns a formatted string.
    Syntax: printf (format,arg1,arg2,arg++)
    parameters format is the converted format, in percent notation ("%") Start to convert character end. The following is the possible format value:
*%%– Returns the percent symbol
*%b– binary number
*%c– characters in accordance with ASCII value
*%d– signed decimal number
*%e– sustainable 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
*%s– string
*%x– hexadecimal number (lowercase letter)
*%x– hexadecimal number (capital letter)
    arg1, arg2, arg++ and other 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:

<?php printf ("My name is%s%s. "," Xshell "," net "); My name is Xshell net. printf ("My name is%1\ $s%1\ $s", "Xshell", "net"); Add 1\$ or 2\$ in front of s ..... Represents the position of the following parameter display, this line outputs My name is Ricky Ricky because only the first parameter is displayed two times. printf ("My name is%2\ $s%1\ $s", "Xshell", "net"); My name is Net Xshell?>

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:

<?php sprintf ("My name is%1\ $s%1\ $s", "Xshell", "net");  You will find nothing to output.  $out = sprintf ("My name is%1\ $s%2\ $s", "Xshell", "net");  echo $out;  Output My name is Xshell net?>

Vi.. Var_dump function
Function: The content, type, and length of the output variable's contents, type, or string. Commonly used to debug.

<?php $a = 100; Var_dump ($a); Int (+) $a = 100.356; Var_dump ($a); Float (100.356)?>

PHP output statement echo, print, Print_r, printf, sprintf, var_dump 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.