Summary of Common Output Functions in PHP

  • 2021-07-18 07:36:48
  • OfStack

echo (); "Output content";

You can output multiple strings, multiple parameters, no parentheses, and no return value.

print (); If there is a return value of 1, 0, it can only contain 1 parameter

You can only output 1 string and 1 parameter at the same time. You need parentheses and have a return value. When its execution fails, you will return flase.
The usage of print is very similar to that of C, so the% in the output will be specially interpreted.


$a=print( ' hi');
echo $a;
// ------ - hi 1 //1  Yes $a The value of. // --------

die (); 1 is generally used to judge the database connection, and the contents after die () will not be executed once die () is executed

die (); The difference between//and exit ().

There are two functions: output the content first, and then exit the program. (Commonly used in linked servers, databases)


mysql_connect( " locahost " , " root " , " root " ) or die( "Linked server failed!" );

printf (); Like the usage of C language 1, the output can be formatted

printf (); //f refers to format formatting

printf ("Parameter 1", Parameter 2): Parameter 1 = what format to output; Parameter 2 = the variable to output.

(% s: By string; % d: By integer type; % b: in binary; % x: in hexadecimal; % X: output in hexadecimal uppercase; % o: in octet; % f: In floating-point mode)
Function, return the number of output characters, and output the text after formatting, such as:


printf ( " $%01.2f " , 43.2); //$43.20

Character for padding
0 means that the number of bits is not enough to be supplemented without affecting the original value
1 represents the total width of the output
2 denotes decimal places, with 4 rounded by 5 entries
% f is indicated as 1 floating point number

Formatting commands and instructions:

%% prints the percentage symbol and does not convert.
% b integer to binary.
% c integer to corresponding ASCII character.
% d integer converted to decimal.
% f times accuracy number converted to floating point number.
% o integer converted to octal.
% s integer to string.
% x integer converted to lowercase 106 carry.
% X integer converted to uppercase 106 carry.


<?php $num=100.001;
printf( " %d " ,$num); //100
printf( " %s " ,$num); //100.001
printf( " %s - %d - %b - %x - %o - %f " ,$num,$num,$num,$num,$num,$num)
//100.001 - 100 - 1100100 - 64 - 144 - 1001.00100
printf( " %.2f " ,$num); //100.00 ( Decimal point preservation 2 Bit )
printf( " %.1f " ,$num); //100.0 ( Decimal point preservation 1 Bit )
printf( " %`#10s " ,$num); //#10s
printf( " %#10s " ,$num); //10s
?>

sprintf; Store the output in variables

This can't be output directly. Assign it to 1 variable first, and then output the variable.


<?php
$num=100.001;
$a=sprintf( " %d " ,$num);
echo $a; //100
?>

print_r (); Used to output arrays

Function: Only used for output arrays.


$a = array (1, 2, array ( " a " , " b " , " c " ));
print_r ($a);

Return to:

Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => a [1] => b [2] => c ) )

var_dump (); You can output anything

Capacity, type of output variable or content, type and length of string. Commonly used for debugging.


<?php
$a=100;
var_dump($a); //int(100) $a=100.356;
var_dump($a); //float(100.356) ?>

var_export ();
Returns structural information about the variables passed to the function, similar to var_dump (), except that the returned representation is a legitimate PHP code.

You can return the value of a variable by setting the second parameter of the function to TRUE.


<?php
$a = array (1, 2, array ( " a " , " b " , " c " ));
var_export ($a);
/*
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
*/
?>

Simplified usage:


<?php
$color = "red";
?> <p>Roses are <?=$color?></p>


Related articles: