PHP Basics Review echo Print printf sprintf print_r var_dump usage and differences

Source: Internet
Author: User
Tags parse error php basics
PHP Basics Review the use and difference of echo print printf sprintf print_r 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:

Copy the Code code as follows:


 Php$a=Echo("55nav");//Error! Cannot be used to assign a valueEcho"55nav";//55navEcho("55nav");//55navEcho("55nav", "com");//An error occurred with parentheses that cannot pass multiple argumentsEcho"55nav", "com", "is", "web";//you can separate multiple values with commas without parentheses, and output a 55nav com is webEcho"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"$fistnameCOM ";//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 the Code code as follows:


 
  $a Print // this is permissible.   Echo $a //  ?>


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 the Code code 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 the Code code as follows:


 
  printf //  printf//printf//  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 the Code code 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: The content, type, and length of the output variable's contents, type, or string. Commonly used to debug.

Copy the Code code as follows:


 
  $a=100var_dump($a//$a=100.356   Var_dump($a//

  • 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.