PHP output String (echo,print,printf,print_r,var_dump) Java printf printf D printf Source code

Source: Internet
Author: User
in PHP, there are four ways to output strings. The ECHO structure can output multiple values at once; print () can output only one value; printf () can format the output; Print_r () can output an array, which is good for debugging.

1. Echo
echo is a keyword in PHP that has no return value. In the notation, it can omit parentheses. The following code:

Echo ' Test String ';
Echo (' Test String ');

2. Print

Print is also a keyword in PHP, it has a return value, generally returns true, the case should not return false. As with Echo, it is possible to omit the parentheses in the notation. The following code:

print ' Test String ';
Print (' Test String ');

3. printf
printf can format the output of a string like the C-language printf. It has the same format as the C language and is preceded by%. Its descriptor is defined as follows.
b parameter is an integer that shows the binary
c parameter is an integer that displays the corresponding ASCII character
d parameter is an integer that displays its decimal
The f parameter is double and is displayed as a floating-point number The
e parameter is double and is displayed as a scientific count
g parameter is double, displayed as a floating-point number or as a scientific count
o parameter is an integer that displays its octal
The s parameter is a string that is displayed as a string
u parameter is an unsigned integer that displays its decimal
The x/x parameter is an integer that displays its hexadecimal (case-insensitive)

% output% to illustrate:
F,e default six digits after the decimal point, g in more than six digits (plus decimal), will be rounded, if the value after rounding is less than 1000000 will be directly output, greater than 1000000 will be displayed as scientific counting type. f The result of a value greater than 1.2e23 output is incorrect.
In addition to the above, the other can specify the total number of outputs (decimal point, E is one), and you can specify 0 or a space as a complement, you can also specify whether the complement is left or right.
F,e can specify the number of digits after the decimal point.
Such as%5d indicates that the total number of output is 5, insufficient left to fill the space,%05d indicates that the total output number is 5, insufficient left 0,%05.1f indicates the total output number is 5, insufficient left to fill 0, 1 digits after the decimal point,%-05.1f indicates the total output number is 5, insufficient right to fill 0, 1 digits after the decimal point;
Example code:

printf ("%7.2f", 1.2); "1.20"
printf ("%-07.2f", 1.2); "1.20000"

4. sprintf
sprintf and format conversion are like printf, the difference is that printf is output directly, and sprintf returns a formatted string.
5. Print_r and Var_dump
Both the Print_r and var_dump can output arrays and objects, but the output of the Print_r to the boolean is not obvious; the var_dump output is verbose and much more commonly used for debugging.
The following code:

The code is as follows:


$v = new test ();
Print_r ($v);
Var_dump ($v);
Class Test {
public $num = 1;
Public $str = "222";
Public $bln = true;


The result is:

Copy the Code code as follows:


Test Object
(
[num] = 1
[str] = 222
[BOOL] = 1
)
Object (Test) #1 (3) {
["Num"]=>
Int (1)
["Str"]=>
String (3) "222"
["BOOL"]=>
BOOL (TRUE)
}


The above describes the PHP output string (echo,print,printf,print_r,var_dump), including the content of the printf, I hope that the PHP tutorial interested in a friend helpful.

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