PHP Learning Output strings (Echo,print,printf,print_r and var_dump) _php basics

Source: Internet
Author: User
Here are the introductions.
1. Echo
echo is a keyword in PHP that does not return a value. In writing, it can omit the parentheses. The following code:
Copy Code code as follows:

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

2. Print
Print is also a keyword in PHP, it has a return value, generally returns True, the return of false should not. In writing, like Echo, it can omit parentheses. The following code:
Copy Code code as follows:

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

3. printf
printf can format the output of a string like the C-language printf. Its format is similar to the C language, and it starts with a%. The descriptor is defined as follows.
The b parameter is an integer, showing the binary
The c parameter is an integer, showing the corresponding ASCII character
The D argument is an integer, showing its decimal
The f parameter is double precision and is displayed as a floating-point number
e parameter is double precision, show as scientific counting type
G parameter is double precision, display as floating point number or scientific count type
o parameter is an integer, showing its octal
The s parameter is a string and is displayed as a string
The U parameter is an unsigned integer that displays its decimal
The x/x argument is an integer, displaying its hexadecimal (case-by-display)
% output% to illustrate:
F,e the default decimal point after six digits, 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 words will be shown into scientific counting type. F is not the result of a value greater than 1.2e23 output.
In addition to%, the other can specify the total number of output digits (decimal, E is counted as a), and you can specify 0 or 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.
For example,%5d indicates that the total number of digits in output is 5, insufficient left complement space;%05d represents a total output of 5, insufficient left complement 0,%05.1f indicates a total output of 5, less than 0 left, 1 decimal point,%-05.1f indicates that the total number of digits of output is 5, insufficient right to 0, the decimal point after 1 digits;
Sample code:
Copy Code code as follows:

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

4. sprintf
sprintf and format conversions are the same as printf, where the difference is in printf direct output, while sprintf returns a formatted string.
5. Print_r and Var_dump
Both Print_r and var_dump can output arrays and objects, but Print_r is less obvious to the Boolean output, and the Var_dump output is more detailed, and the general debugging is much more.
The following code:
Copy Code code as follows:

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

The results are:
Copy 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)
}

Resources:
PHP Programming, 2003, fourth string, output string

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.