Echo (), print (), print_r () usage in php

Source: Internet
Author: User
My understanding of the echo (), print (), print_r () function is that echo can input string variable constants, and print is similar to echo, however, print_r can print arrays, but the first two cannot,

My understanding of the echo (), print (), print_r () function is that echo can input string variable constants, and print is similar to echo, however, print_r can print arrays, but the first two are not. I will introduce the usage and differences of the three.

Echo is a PHP statement, print and print_r are functions, and the statement does not return values. the function can return values (even if it is not used)

Print () can only print values of simple type variables (such as int and string)

Print_r () can print values of complex types of variables (such as arrays and objects)

Echo outputs one or more strings

Echo-output one or more strings: Echo (string arg1 [, string...]). the returned value is null. the code is as follows: echo "Hello", "friend ";

Print-output a string:Int print (string arg). the returned value is an integer. the code is as follows: print "Hello Friend ";

You can perform the following operations:

  1. $ Name = print "nihao n ";
  2. $ Str = 'Test print value is '. $ name;
  3. Eval ("$ print =" $ str ";");
  4. Echo $ print;

Print_r-prints easy-to-understand information about variables.

Bool print_r (mixed expression [, bool return]) // The return value is Boolean and the parameter type is mix. it can be a string, an integer, an array, and an object class print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.

Print_r () will move the array pointer to the last edge. you can.

  1. Print_r (str );
  2. Print_r (int );
  3. Print_r (array );
  4. Print_r (obj );

The following are examples of the four methods that can output strings.

Echo, print (), printf (), print_r ()

EchoMultiple values can be output at a time. multiple values are separated by commas. echo is a language construct rather than a real function. Therefore, it cannot be used as part of an expression.

Syntax: echo "Hello", "World"; syntax error: echo ("Hello", "World ");

Print () function,Print () prints a value (its parameter). If the string is successfully displayed, true is returned. otherwise, false is returned. the code is as follows:

  1. If (! Print ("Hello, World ")){
  2. Die ("you are not listening to me ");
  3. }

Printf (),Printf () is derived from printf () in C language (). This function outputs formatted strings.

Syntax: printf (format, arg1, arg2, arg ++)

Format specifies the string and how to format the variable;

Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, insert arg2 at the second % symbol, and so on. The code is as follows:

  1. $ Str = "Hello ";
  2. $ Number = 123;
  3. Printf ("% s world. Day number % u", $ str, $ number );

If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "$". See the example code below:

  1. $ Number = 123;
  2. Printf ("With 2 decimals: % 1 $. 2fbr/> With no decimals: % 1 $ u", $ number );

Print_r () and var_dump ()

Print_r () can print strings and numbers, while the Array is displayed in the form of an enclosed key and a list of values, and starts with Array. the code is as follows:

  1. $ A = array ('name' => 'Fred ', 'age' => '15', 'wife' => 'wilm ');
  2. Print_r ($ );
  3. Output: Array
  4. {
  5. [Name] => Fred
  6. [Age] => 15
  7. [Wife] => Wilma
  8. }

The code is as follows:

  1. Class P {
  2. Var $ name = 'Nat ';
  3. //...
  4. }
  5. $ P = new P;
  6. Print_r ($ p );
  7. Output: Object
  8. {
  9. [Name] => nat
  10. }

But print_r () outputs Boolean values and NULL results are meaningless, because both print "n", so var_dump () function is more suitable for debugging.

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.