PHP learning output strings of echo print printf print_r and var_dump

  • 2020-03-31 21:37:26
  • OfStack

The following is an introduction.
1. Echo
Echo is a keyword in PHP that does not return a value. It can be written without the parenthesis. The following code:
 
echo 'Test String'; 
echo('Test String'); 

2. Print
Print is also a key in PHP, and it has a return value, which normally returns true, but not false. In writing, like echo, you can omit the parentheses. The following code:
 
print 'Test String'; 
print('Test String'); 

3. The printf
Printf can format the output as a string, just like printf in C. Its format is similar to that of C, starting with %. Its specifier is defined as follows.
B parameter is an integer, showing its binary value
The c parameter is an integer and the corresponding ASCII character is displayed
The d parameter is an integer, showing its decimal value
The f parameter is double precision and is displayed as a floating point number
E parameter is double precision and is shown as scientific counting type
The g parameter is double precision and is shown as floating point or scientific counting type
The o parameter is an integer and its octal value is displayed
The s parameter is a string and is displayed as a string
The u parameter is an unsigned integer, showing its decimal value
The x/ x parameter is an integer and is shown in hexadecimal (case)
% output % to explain is:
F,e defaults to six decimal places, g will be rounded when more than six (plus the decimal point), if the rounded value is less than 1000000 will be directly output, more than 1000000 will be shown as scientific counting. The output result of f at value greater than 1.2e23 is not correct.
With the exception of %, you can specify the total number of output digits (one decimal, one E), you can specify 0 or space as a complement, and you can specify whether the complement is left or right.
F, e can specify the number of decimal places.
If %5d means that the total number of output digits is 5, not enough left space; %05d means that the total number of output digits is 5, which is insufficient to supplement 0. %05.1f means that the total number of output digits is 5, which is insufficient to fill 0 to the left and 1 place after the decimal point; %-05.1f means that the total number of output digits is 5, not enough to fill the right 0, 1 place after the decimal point;
Sample code:
 
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, except 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 is less obvious for Boolean output. The output of var_dump is more detailed and is generally used for debugging.
The following code:
 
$v = new test(); 
print_r($v); 
var_dump($v); 
class test { 
public $num = 1; 
public $str = "222"; 
public $bln = true; 

The result is:
 
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 strings, output strings

Related articles: