PHP learning output strings (echo, print, printf, print_r and var_dump)

1. Echo is a keyword for PHP that has no return value. In writing, it can omit the parentheses. The following code: Copy the code as follows: Echo 'Test String'; Echo('Test String'); Print Print is also a keyword for PHP, it has a return value, generally returns true, and returns false should not. In writing, it is the same as echo, you can omit the parentheses. The following code: Copy the code as follows: Print 'Test String'; Print('Test String'); 3. printf Printf can format and output a string like the C printf. Its format is similar to C, starting with %. Its specifiers are defined as follows. b parameter is an integer, showing its binary c parameter is an integer, showing the corresponding ASCII characters d parameter is an integer, showing its decimal The f parameter is double precision and is displayed as a floating point number. The e parameter is double precision and is displayed as scientific counting type. g parameter is double precision, displayed as floating point number or scientific counting type o parameter is an integer, showing its octal s parameter is a string, displayed as a string u parameter is an unsigned integer, showing its decimal The x/X argument is an integer and its hexadecimal is displayed (indicated by case) % output % to be explained is: f, e defaults to six decimal places. When g exceeds six digits (plus a decimal point), it will be rounded off. If the value after rounding is less than 1000000, it will be directly output. If it is greater than 1000000, it will be displayed as scientific count. The result of f outputting at a value greater than 1.2e23 is incorrect. In addition to the above, all other can specify the total number of output bits (decimal point, E counts one bit), and can specify 0 or space as a complement, and 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 means that the total number of output digits is 5, less than the left complement space; %05d means that the total number of output digits is 5, less than the left complement 0; %05.1f means that the total number of output digits is 5, less than 0 for the left, 1 after the decimal point Bit; %-05.1f means that the total number of outputs is 5, less than 0 for the right, and 1 after the decimal point; Sample code: Copy the code as follows: Printf("%7.2f", 1.2); // " 1.20" Printf("%-07.2f", 1.2); // "1.20000" 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 is less obvious for Boolean output; var_dump output is more detailed and generally much more useful for debugging. The following code: Copy the code 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 as follows: Test object ( [num] => 1 [str] => 222 [bool] => 1 ) Object(test)#1 (3) { ["num"]=> Int(1) ["str"]=> String(3) "222" ["bool"]=> Bool(true) }