The difference between PHP output echo, print, Print_r, printf, sprintf, var_dump _php skills

Source: Internet
Author: User
Tags parse error sprintf
With. NET development has been 5 years, recently suddenly want to contact. NET, so let's take a look at PHP. In learning PHP, first look at a few 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 using parentheses. and echo returns void and does not return a value, so it cannot be used to assign a value.
Example:
Copy Code code 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 not passing multiple arguments
echo "55nav", "com", "is", "web"; You can separate multiple values with commas without parentheses, and output the 55nav COM is web
echo "55nav is 8 good 9 web."; The end display is good web for a row of 55nav is, regardless of whether the line is wrapped.
$fistname = "55nav";
echo "$fistname com"; If $firstname = "55nav", 55nav com is output.
echo ' $firstname com '; Because of the use of single quotes, $firstname values are not exported, but output is $firstname com
?>

Second, print
Print () is the same as Echo (), but the echo speed is a little bit faster than print. It's not actually a function, so you don't have to use parentheses about it. However, if you want to pass more than one parameter to print (), a parse error occurs using parentheses. Note that print always returns 1, which is not the same as ECHO, which means you can use print to assign values, but it doesn't make sense.
Example:
Copy Code code 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 easily understandable information about a variable.
Syntax: Mixed print_r (mixed $expression [, BOOL return])
If the variable is a string, integer or float will output its value directly, and if the variable is an array, it will output a formatted array for readability, that is, the format of the key and value. is similar to object objects. Print_r has two parameters, the first is a variable, the second can be set to true, and if set to true, the string is returned, otherwise the Boolean value is true.
Example:
Copy Code code as follows:

<?php
$a = "55nav";
$c = Print_r ($a);
Echo $c; The value of the $c is true
$c = Print_r ($a, true);
Echo $c; $c value is a string of 55nav
?>

Four, printf function
The printf function returns a formatted string.
Syntax: printf (format,arg1,arg2,arg++)
The parameter format is the format of the transformation, starting with the percent sign ("%") to the end of the converted character. The following are the possible format values:
*%%– return percent symbol
*%b– binary number
*%c– characters according to ASCII values
*%d– Signed decimal number
*%e– continuous counting method (e.g. 1.5e+3)
*%u– unsigned decimal number
*%f– floating-point number (Local settings Aware)
*%f– floating-point number (not local settings aware)
* Number of%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 by percent semicolon (%) Symbol Place. The function is executed progressively, in the first% symbol, insert arg1, at the second% symbol, insert arg2, and so on. If the% symbol is more than the arg parameter, you must use a placeholder. After the placeholder is inserted into the% symbol, it is made up of numbers and "\$." You can use numbers to specify which parameters to display, see examples for details.
Example:
Copy Code code 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\$ before s ... Represents the location of the following parameter display, this row outputs the I 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 Code code as follows:

<?php
sprintf ("My name is%1\ $s%1\ $s", "55nav", "com"); You will find that there is nothing to output.
$out = sprintf ("My name is%1\ $s%2\ $s", "55nav", "com");
Echo $out; Output My name is 55nav com
?>

Six, var_dump function
Function: The content, type, and length of the output variable's content, type, or string. Commonly used to debug.
Copy Code code as follows:

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

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.