Differences in several output functions in PHP

Source: Internet
Author: User
The difference between several output functions in PHP

?

Echo : The return value is void, you can output multiple values at once, and multiple values are separated by commas. Echo is a language structure (language construct) , which is a keyword, not a real function, and therefore cannot be used as part of an expression. Use without parentheses, plus you can. Only basic types are supported, except for Boolean, whenecho True shows 1,echo false has nothing at all. echo can not output array type;

Print : The return value is int, which can output only one string at a time, a parameter, a parenthesis, a syntax that does not support commas separating multiple display variables, and a return value when execution fails flase. The use of print is similar to the C language, so there is a special explanation for the % of output content. print can output array types of data;

Print_r : You can print not only the values of variables, but also the types of variables, as well as the complex types of variables such as arrays and objects. print_r () Moves the pointer of the array to the last edge. Use reset () to get the pointer back to the beginning, Print_r () to simply print the string and the number, and the array to be displayed in the enclosed key and the list of merits, with the array start with;

printf : from printf () in the C language. The function outputs a formatted string. Syntax: printf (" parameter 1", parameter 2) : parameter 1 = output by what format; parameter 2 = output variable. (%s: by string ;%d: by integral type ;%b: by binary; %x: Press + to enter; % x : Press + to capitalize output; %o: by octal ;%f: by float ) . After the placeholder is inserted into the % symbol, it consists of a number and a "\$" . F=format : Specifies the string and how to format the variables in it, as follows: printf ("$%01.2f", 43.2),//$43.20 ; $ denotes populated characters; 0 indicates that the number of digits is not sufficient to complement the 0 without affecting the original value; 1 indicates the total width of the output; %f is displayed as a floating point;

sprintf : This cannot be output directly , first assigning a variable, and then outputting the variable. as follows:

   

?

Var_dump : void Var_dump (mixed expression [, mixed expression [, ...]]) function: Output variable content, type, or string content, type, length. Commonly used to debug. as follows:

  

?

Var_export : mixed Var_export (mixed $expression [, BOOL $return]) returns the structure information about the variable passed to the function, similar to var_dump () , unlike the PHP code whose returned representation is valid. You can return the value of a variable by setting the second argument of the function to TRUE. as follows:

  

 
  

?

Output:Array (0 = = + = 2,2 = Array (0 = ' a ', 1 = ' B ', 2 = ' C ',),),3.1;

Summary: Echo ,print,printf can print variable content, but cannot display arrays and system super variable arrays;print_r and var_dump can not only print arrays, A scalar variable that can also print the contents of an object.

?

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