1. Echo Function:
The output function , which is a command, cannot return a value. Echo can be followed by a number of parameters, separated by semicolons, such as:
echo $myvar 1;
Echo, $myvar, "<b>bold</b>";
2. print Function:
is a function that returns a value that can have only one parameter.
int print (string arg)
Outputs arg . Returns 1 , always.
3. printf function:
int printf (string format [, mixed args [, mixed ...])
Produces output according format to, which are described in the documentation for sprintf () .
Returns the length of the outputted string.
The text is formatted for later 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 does not print, but returns formatted text, the rest is the same as printf.
5. The printf () function is explained in detail:
The format for calling the printf () function is:
printf ("< formatted string >", < Parameter table >);
%d decimal signed integer
%u decimal unsigned integer
%f floating Point
%s string
%c single character
%p the value of the pointer
Floating-point numbers in the form of%e indices
%x,%x unsigned integer in hexadecimal notation
%o unsigned integer represented in octal
%g automatic selection of appropriate representations
Description
(1). You can insert a number between "%" and a letter to indicate the maximum field width.
① For example:%3d represents the output of 3-bit integers, not enough 3-bit right-justified.
The ②%9.2f represents a floating-point number with an output field width of 9, where the decimal bit is 2, the integer digit is 6, the decimal point is one, and not enough 9-bit right-aligned.
The ③%8s represents the output of a 8-character string, which is not aligns aligned to 8 characters.
④ if the length of the string or the number of integers exceeds the width of the description, the output is based on its actual length.
⑤ floating point number, if the integer number of bits exceeds the width of the indicated integer digits, the actual integer digits will be output;
⑥ fractional digits exceed the indicated decimal width, the output is rounded by the width of the description.
⑦ If you want to add some 0 before the output value, add a 0 before the field width.
For example,%04d means that when you output a value that is less than 4 bits, it will be preceded by 0 to make its total width 4 bits.
⑧ If you use floating-point numbers to represent the output format of a character or integer, 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 indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If it is greater than 9, the contents of the 9th character will be deleted.
(2). You can add a lowercase letter L between "%" and the letter, indicating that the output is a long form.
① For example:%ld indicates an output long integer
②%LF indicates the output double floating-point number
(3). You can control the output left or right alignment, that is, "%" and the letter by adding a "-" sign to indicate that the output is left-justified, otherwise it is right-justified.
① For example:%-7d indicates output 7-bit integer left justified
②%-10s indicates output 10 characters aligns aligned
(4). Some special provisions of the character
①\n line break
②\f Clear screen and change page
③\r Enter
④\t Tab character
⑤\XHH represents an ASCII code with 16-in notation,
⑥ where HH is 1 to 2 x 16 decimal
6. printf () : examples
Example 1: various examples
<?php<br>
$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:zero-padded integers
<?php $isodate = sprintf( "%04d-%02d-%02d" , $year , $month , $day ); ?> |
Example 4:formatting currency
<?php $money 1 = 68.75; $money 2 = 54.35; $money = $money 1 + $money 2 ; //echo $money would output "123.1"; $formatted = sprintf ( "%01.2f" $money ; //echo $formatted would output "123.10" ?> |
Example 5: sprintf () : Scientific notation
<?php $number = 362525200; echo sprintf( "%.3e" , $number ); // outputs 3.63e+8 ?>
|
PHP Echo, print, printf, sprintf function differences and use