echo, print, print_r differences in PHP

Source: Internet
Author: User
echo, print, print_r differences in PHP

Overview:

All three can output information, but each has its own characteristics:

Type Number of output variables Output Variable Type return value Speed
Echo Language structure One or more Simple type variables, such as int, string No The fastest
Print Language structure One Simple type variables, such as int, string Int Fast
Print_r Function One Complex types, such as arrays, objects bool Slow

Echo is not a function, but a PHP statement that can output multiple variables with a comma interval and has no return value, which is also the fastest.

 
  echo "Xyw_", "Eliot", "Blog"; Output Xyw_eliot Blogecho ("Xyw_", "Eliot", "Blog");//Compile error, parentheses cannot pass multiple parameters $name = "Xyw_eliot"; echo "$name is a blog!"; /Output Xyw_eliot is a Blog!echo ' $name is a blog! '; /Output $name is a blog!//double quotation marks will parse the internal variables, output the contents of the variables, and the single quotation marks will not parse the variables, but output them as-is?>
The use of print and Echo is basically the same, but print can only output one variable and has a return value that returns 1 if the output succeeds.

 
  Print "Xyw_eliot is a blog!\n";//Output Xyw_eliot is a blog!//print "Xyw_eliot", "is a blog!"; /Compile error, print cannot pass multiple parameters $return = print "Xyw_eliot is a blog!\n";//print successfully, return 1echo $return;//Output 1?>
Print_r is a function that prints easy-to-understand information about variables. If the argument is a string, integer, or float, the variable value itself is printed. If the parameter is an array, the keys and elements are displayed in a certain format. object is similar to an array.

Print_r has two parameters, the first is a variable, the second can be set to true, and if set to True, returns the variable to be printed, otherwise returns a Boolean value of true.

 
  "Xyw", "2" = "Eliot", "3" = "blog");p Rint_r ($arr);//output array $return =print_r ($arr);//return value is 1echo $return;//Output 1$ Return =print_r ($arr, true);//return array echo $return;//output Array?>
Output:

Array
(
[1] = Xyw
[2] = Eliot
[3] = = Blog
)

This article is Eliot original, reprint please indicate source: http://blog.csdn.net/xyw_blog/article/details/13743341

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