PHP echo print printf sprintf functions

  • 2020-11-20 06:03:06
  • OfStack

1. echo function:

Output function, is a command, cannot return value. echo can be followed by a number of parameters separated by semicolons, such as:
echo $myvar1;
echo 1,2,$myvar," < b > bold < /b > ";


2. print function:

Is a function that returns 1 value and has only 1 argument.

int print ( string arg )

Outputs arg . Returns 1 , always.


3. printf function:

int printf ( string format [, mixed args [, mixed ...]] )

Produces output according to format , which is described in the documentation for sprintf() .

Returns the length of the outputted string.


Format the text for output, such as:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);


4. sprintf function:
string sprintf ( string format [, mixed args [, mixed ...]] )

Returns a string produced according to the formatting string format .


Similar to printf, but not printed, but returns formatted text, the rest as printf1.


5. Explain printf() function in detail:

The call format of the printf() function is:
printf(" < Formatted string > ", < Parameter form > );


%d 10 decimal signed integer
%u decimal unsigned integer
% f floating point number
% s string
%c single character
%p pointer value
%e floating-point number in exponential form
%x, %X Unsigned integer in base 106
%o An unsigned integer expressed in base 8
%g automatically selects the appropriate notation


Description:

(1). A number can be inserted between "%" and letters to indicate the maximum field width.

For example: %3d means output 3 bits integer number, not enough 3 bits right alignment.

f represents a floating-point number with an output field width of 9, in which the decimal is 2, the integer is 6, and the decimal is 1, not enough for the right alignment of 9 digits.

%8s represents a string that outputs 8 characters, not enough to justify to the right.

If the length of the string, or the integer digit exceeds the specified field width, the actual length will be output.

(5) Floating-point number, if the integer part digit exceeds the specified integer bit width, the actual integer bit will be output;

The decimal digit exceeds the specified decimal width, the output is rounded to 5 at the specified width.

If you want to add a 0 before the output value, you should add a 0 before the field width term.

For example: %04d means that when a value less than 4 bits is printed, 0 will be added in front to make its total width 4 bits.

If floating-point Numbers are used to represent the output format of characters or integers, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.

For example: %6.9s means to display a string of length not less than 6 and not greater than 9. If it is greater than 9, everything after the ninth character will be deleted.


(2). The lowercase letter l can be added between "%" and letters to indicate that the output is the length number.

For example: %ld means output long integer

%lf means output double floating-point number


(3). The output can be left aligned or right aligned, that is, a "-" sign between "%" and letters can indicate that the output is left aligned, otherwise it is right aligned.

For example: % -7ES150en means output 7 bit integer left alignment

%-10s means output 10 characters left aligned


(4). 1 some special specified characters

(1)/n line feed
f clear screen and change page
(3)/r enter
(4)/t Tab operator
/xhh means that one ASCII code is represented by 16 decimal digits,
hh is one to two hexadecimal Numbers

6. printf() : examples

Example 1: various examples


<?php  
$n =  43951789;  
$u = -43951789;  
$c = 65; // ASCII 65 is 'A'  

// notice the double %%, this prints a literal '%' character  
printf("%%b = '%b'/n", $n); // binary representation  
printf("%%c = '%c'/n", $c); // print the ascii character, same as chr() function  
printf("%%d = '%d'/n", $n); // standard integer representation  
printf("%%e = '%e'/n", $n); // scientific notation  
printf("%%u = '%u'/n", $n); // unsigned integer representation of a positive integer  
printf("%%u = '%u'/n", $u); // unsigned integer representation of a negative integer  
printf("%%f = '%f'/n", $n); // floating point representation  
printf("%%o = '%o'/n", $n); // octal representation  
printf("%%s = '%s'/n", $n); // string representation  
printf("%%x = '%x'/n", $n); // hexadecimal representation (lower-case)  
printf("%%X = '%X'/n", $n); // hexadecimal representation (upper-case)  

printf("%%+d = '%+d'/n", $n); // sign specifier on a positive integer  
printf("%%+d = '%+d'/n", $u); // sign specifier on a negative integer  
?>   

  

The printout of this program would be:   
%b = '10100111101010011010101101'  
%c = 'A'  
%d = '43951789'  
%e = '4.39518e+7'  
%u = '43951789'  
%u = '4251015507'  
%f = '43951789.000000'  
%o = '247523255'  
%s = '43951789'  
%x = '29ea6ad'  
%X = '29EA6AD'  
%+d = '+43951789'  
%+d = '-43951789' 

Example 2: string specifiers

<?php  
$s = 'monkey';  
$t = 'many monkeys';  

printf("[%s]/n",      $s); // standard string output  
printf("[%10s]/n",    $s); // right-justification with spaces  
printf("[%-10s]/n",   $s); // left-justification with spaces  
printf("[%010s]/n",   $s); // zero-padding works on strings too  
printf("[%'#10s]/n",  $s); // use the custom padding character '#'  
printf("[%10.10s]/n", $t); // left-justification but with a cutoff of 10 characters  
?>   

The printout of this program would be:   
[monkey]  
[    monkey]  
[monkey    ]  
[0000monkey]  
[####monkey]  
[many monke] 

Example 3: ES187en-ES188en integers

<?php  
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);  
?>  

Example 4: formatting currency

<?php  
$money1 = 68.75;  
$money2 = 54.35;  
$money = $money1 + $money2;  
// echo $money will output "123.1";  
$formatted = sprintf("%01.2f", $money);  
// echo $formatted will output "123.10"  
?> 

Example 5: sprintf() : scientific notation

<?php  
$number = 362525200;  

echo sprintf("%.3e", $number); // outputs 3.63e+8  
?>  


Related articles: