Home > Backend Development > PHP Tutorial > PHP learning output string (echo, print, printf, print_r and var_dump)_PHP tutorial

PHP learning output string (echo, print, printf, print_r and var_dump)_PHP tutorial

WBOY
Release: 2016-07-21 15:30:42
Original
750 people have browsed it

They are introduced one by one below.
1. echo
echo is a keyword in PHP, it has no return value. In terms of writing, it can omit the parentheses. The following code:

Copy code The code is as follows:

echo 'Test String';
echo('Test String' ; In terms of writing, it is the same as echo, and the parentheses can be omitted. The following code:

Copy code
The code is as follows:
print 'Test String'; print('Test String' );
3. printf

printf can format and output a string like printf in C language. Its format is similar to that of C language, both starting with %. Its specifier is defined below.
b The parameter is an integer and its binary representation is displayed
c The parameter is an integer and its corresponding ASCII character is displayed
d The parameter is an integer and its decimal representation is displayedf The parameter is double precision and its floating point number is displayed
e The parameter is double precision, displayed as scientific notation
g The parameter is double precision, displayed as floating point number or scientific notation
o The parameter is an integer, displayed in octal
s The parameter is a string, displayed as String
u parameter is an unsigned integer, display its decimal format
x/X parameter is an integer, display its hexadecimal format (displayed in upper and lower case respectively)
% Output % should be explained:
f and e default to six decimal places. When g exceeds six decimal places (plus decimal point), it will be rounded. If the rounded value is less than 1000000, it will be output directly. If it is greater than 1000000, it will be displayed in scientific notation. When the value of f is greater than 1.2e23, the output result is incorrect.
Except for %, all of the above can specify the total number of output digits (the decimal point and E count as one), and you can specify 0 or a space as the padding character, and you can also specify whether the padding is on the left or right.
f, e can specify the number of decimal places.
For example, %5d means that the total number of output digits is 5, and the remaining digits are left padded with spaces; %05d means that the total number of output digits is 5, and the remaining digits are left padded with 0s; %05.1f means that the total number of output digits is 5, and the remaining digits are left padded with 0s. 1 digit after the decimal point; %-05.1f means that the total number of output digits is 5, and any missing digits are padded with 0 on the right and 1 digit after the decimal point;
Sample code:



Copy code

The code is as follows:
printf("%7.2f", 1.2); // " 1.20" printf("%-07.2f", 1.2); // "1.20000 "
4. sprintf

sprintf and format conversion are the same as printf. The difference between the two is that printf outputs directly, 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’s output of Boolean types is not obvious; var_dump’s output is more detailed and is generally used during debugging.
The following code:

Copy 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 code

The code is as follows:
test Object ( [num] => 1 [str] => 222
[bool] => 1
)
object(test)#1 (3) {
[ "num"]=>
int(1)
["str"]=>
string(3) "222"
["bool"]=>
bool (true)
}


Reference:
PHP Programming, 2003, Chapter 4 String, Output String


http://www.bkjia.com/PHPjc/323157.html

www.bkjia.com

true

TechArticleThe following will be introduced one by one. 1. echo echo is a keyword in PHP, it has no return value. In terms of writing, it can omit the parentheses. The code is as follows: Copy the code The code is as follows: echo 'T...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template