Differences between echo (), print (), and print_r () in PHP

Source: Internet
Author: User
Echo (), print (), and print_r () in PHP. The difference in print is that echo can output multiple variables & #20540; while print only has one variable, output as a string. another difference is that echo does not return & #20540;, and print has the difference of echo (), print (), and print_r () in 1.pr PHP.
The difference between echo and print is that echo can output multiple variable values, while print only has one variable, which is output as a string. another difference is that echo has no return value, and print has a return value. print cannot output arrays and objects.


Print_r can output stirng, int, float, array, object, etc. The structure is used to output the array, and true is returned when the print_r output is successful;
In addition, print_r can use print_r ($ str, true) to make print_r output and return the value after print_r processing.
 
It can be understood:
Print is a print string

Print_r prints composite types such as array objects.

In PHP, the execution speed is from fast to slow: echo (), print (), print_r ()

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

Descr resume ption
Void echo (string arg1 [, string...]) // the returned value is null.

Echo "Hello", "friend ";
Print -- output a string

Descr resume ption
Int print (string arg) // The return value is an integer.
Print "your good friend ";
 
You can perform the following operations:
$ Name = print "nihao \ n ";
$ Str = 'Test print value is $ name .';
Eval_r ("\ $ print = \" $ str \";");
Echo $ print;
 
Print_r -- print 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 () moves the array pointer to the last edge.


You can
Print_r (str );
Print_r (int );
Print_r (array );
Print_r (obj );
You can also use var_dump and var_export.






Echo ()
Multiple strings can be output at the same time, and multiple parameters are allowed. parentheses are not required and no return value is required.
 
Print ()
Only one string and one parameter can be output simultaneously. parentheses are required and return values. if the execution fails, false is returned. the usage of print is similar to that of C language, so it will make a special explanation of % in the output content.
$ A = print ('hi ');
Echo $;
//----------------------------
Hi 1 // 1 is the value of $.
//-----------------------------


Die (); // is different from exit.
There are two functions: output the content first, and then exit the program. (Commonly used in linking servers and databases)
Mysql_connect ("locahost", "root", "root") or die ("link server failed! ");


Printf (); // f indicates format
Printf ("parameter 1", parameter 2): parameter 1 = output in what format; parameter 2 = output variable. (% S: by string; % d: by integer; % B: By binary; % x: by hexadecimal; % X: by hexadecimal; % o: by octal; % f: by floating point)
For parameter 1, the format is as follows:
% ['Padding _ character] [-] [width] [. precision] type


Note:
All conversions start with %. to print a %, "%" must be used ";
The padding_character parameter is optional and is used to fill the variable until the specified width, for example, printf ("$ % 'a10. 2f ", 43.2); // $ aaaaa43.20. by default, a space is filled. if a space is specified or 0, you do not need to use" '"as the prefix. Single quotation marks must be specified for any other prefix.
[-] Is optional. adding it indicates that the data should be left aligned. Instead of the default right alignment. in the preceding example, the value is: printf ("$ % 'a-10.2f", 43.2); // $43.20 aaaaa
Whidth indicates the space left by the replaced variable (calculated by character ). 10 (including decimal point) of the preceding example ).
Precision must start with a decimal point, indicating the number of digits to be displayed after the decimal point.
Function, returns the number of output characters. after formatting the text, it is output, for example:
Printf ("$. 2f", 43.2); // $43.20
$ Indicates the character to be filled
0 indicates that the number of digits is not enough. if the original value is not affected, add 0.
1 indicates the total output width.
2 indicates the number of decimal places, rounded down
% F indicates a floating point number.
 
Formatting commands and instructions:
% Indicates the percentage, which is not converted.


% B integer to binary.
The % c integer is converted to the corresponding ASCII character. For example: printf ("$ % c", 65); // Output:
% D integer to decimal place. For example: printf ("$ % d", 65.53); // output: 65
% F times the precision number to a floating point number.
% O integer to octal.
% S integer to string.
% X integer to lowercase hexadecimal.
% X integer to uppercase hexadecimal
 
For printf (), you can also specify the order of parameter conversion by using the parameter method with the serial number ending with the $ symbol. For example:
Printf ("the total is $ % 2 $. 2f and subtotal: % 1 $. 2f", 65.55, 37.2); // the total is $37.20 and subtotal: 65.55


As shown above: % 2 $. 2f indicates that the second parameter 65.55 is used, and % 1 $. 2f indicates that the first parameter 37.20 is used.
Sqlmap file for database table fields and class attributes 4. create src \ main \
Related Article

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.