The difference between the PHP output echo, print, Print_r, printf, sprintf, Var_dump

Source: Internet
Author: User
Tags parse error sprintf

This article is the PHP output echo, print, Print_r, printf, sprintf, var_dump the difference between the detailed analysis of the introduction, the need for a friend reference With. NET development has been 5 years, and recently suddenly want to touch. NET, so I'll take a look at PHP. In learning PHP first look at several output functions.
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:
Copy CodeThe code is as follows:
<?php
$a = Echo ("55nav"); Error! Cannot be used to assign a value
echo "55nav"; 55nav
Echo ("55nav"); 55nav
Echo ("55nav", "com"); An error occurred with parentheses that cannot pass multiple arguments
echo "55nav", "com", "is", "web"; You can separate multiple values with commas without parentheses, and output a 55nav COM is web
echo "55nav is 8 good 9 web."; The final display is for a row of 55nav is good web, regardless of whether the line is wrapped.
$fistname = "55nav";
echo "$fistname com"; If $firstname = "55nav", 55nav com is output.
echo ' $firstname com '; Because the single quotation marks are used, the $firstname value is not output, but the output $firstname com
?>
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:
Copy CodeThe code is as follows:
<?php
$a = print ("55nav"); This is permissible.
echo $a; The value of $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:
Copy CodeThe code is as follows:
<?php
$a = "55nav";
$c = Print_r ($a);
Echo $c; The value of $c is True
$c = Print_r ($a, true);
Echo $c; The value of $c is the string 55nav
?>
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:
Copy CodeThe code is as follows:
<?php
printf ("My name is%s%s. "," 55nav "," com "); My name is 55nav com.
printf ("My name is%1\ $s%1\ $s", "55nav", "com"); Add 1\$ or 2\$ in front of s ..... Represents the position of the following parameter display, this line outputs My name is 55nav 55nav because only the first parameter is displayed two times.
printf ("My name is%2\ $s%1\ $s", "55nav", "com"); My name is COM 55nav
?>
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:
Copy CodeThe code is as follows:
<?php
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: The content, type, and length of the output variable's contents, type, or string. Commonly used to debug.
Copy CodeThe code is as follows:
<?php
$a = 100;
Var_dump ($a); Int (100)
$a = 100.356;
Var_dump ($a); Float (100.356)
?>

The difference between the PHP output echo, print, Print_r, printf, sprintf, Var_dump

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.