Php output functions: print, print_r, printf, sprintf, die, echo, var_dump, and var_export
1 echo ()
Multiple strings can be output at the same time, and multiple parameters are allowed. parentheses are not required and no return value is required.
2 print ()
Only one string and one parameter can be output simultaneously. parentheses are required and return values. flase is returned when execution fails.
The usage of print is similar to that of C language, so it will make a special explanation of % in the output content.
$ A = print ('hi ');
Echo $;
//????????? -
Hi 1 // 1 is the value of $.
//??????????
3 die (); // is different from exit.
There are two functions: output the content first, and then exit the program. (Commonly used in linking servers and databases)
Mysql_connect ("locahost", "root", "root") or die ("
An error occurred while connecting to the server!
");
4 printf (); // f indicates format formatting
Printf ("parameter 1", parameter 2): parameter 1 = output in what format; parameter 2 = output variable.
(% S: by string; % d: by integer; % B: By binary; % x: by hexadecimal; % X: by hexadecimal; % x: by hexadecimal; % o: by octal; % f: by floating point)
Function, returns the number of output characters. after formatting the text, it is output, for example:
Printf ("$ % 01.2f", 43.2); // $43.20
$ Indicates the character to be filled
0 indicates that the number of digits is not enough and the original value is not affected.
1 indicates the total output width.
2 indicates the number of decimal places, rounded down
% F indicates a floating point number.
Formatting commands and instructions:
% Indicates the percentage, which is not converted.
% B integer to binary.
The % c integer is converted to the corresponding ASCII character.
% D integer to decimal place.
% F times the precision number to a floating point number.
% O integer to octal.
% S integer to string.
% X integer to lowercase hexadecimal.
% X integer to uppercase hexadecimal.
$ Num = 100.001;
Printf ("% d", $ num); // 100
Printf ("% s", $ num); // 100.001
Printf ("% s? % D? % B? % X? % O? % F ", $ num)
// 100.001? 100? 1100100? 64? 144? 1001.00100
Printf ("%. 2f", $ num); // 100.00 (2 decimal places)
Printf ("%. 1f", $ num); // 100.0 (one decimal point)
Printf ("% '# 10s", $ num); // #10 s
Printf ("% # 10s", $ num); // 10 s
?>
5 sprintf ();
This cannot be output directly. assign a variable first and then output the variable.
$ Num = 100.001;
$ A = sprintf ("% d", $ num );
Echo $ a; // The value 100.
?>
6 print_r ();
Function: only outputs an array.
$ A = array (1, 2, array ("a", "B", "c "));
Print_r ($ );
Return value:
Array ([0] => 1 [1] => 2 [2] => Array ([0] => a [1] => B [2] => c))
7 var_dump ();
Function:
The content, type, and length of the output variable. It is often used for debugging.
$ A = 100;
Var_dump ($ a); // int (100)
$ A = 100.356;
Var_dump ($ a); // float (100.356)
?>
8. var_export ();
Returns the structure information about the variable passed to the function. it is similar to var_dump (). The difference is that the returned table is a valid PHP code.
You can return the value of a variable by setting the second parameter of the function to TRUE.
$ A = array (1, 2, array ("a", "B", "c "));
Var_export ($ );
/*
Output:
Array (
0 => 1,
1 => 2,
2 =>
Array (
0 => 'A ',
1 => 'B ',
2 => 'C ',
),
)
*/
$ B = 3.1;
$ V = var_export ($ B, TRUE );
Echo $ v;
/*
Output:
3.1
*/
?>