the commonly used string output in PHP is: use the Echo and print two methods to display the string. You can also try the printf and sprintf functions if the format output operation is involved.
First, say Echo and print two ways
- The syntax format for print is:int print ( string
$arg
)
- Echo's syntax format is:void echo ( string
$arg1
[, string $...
])
Echo is a function-like output, not a function, no return value, and supports multiple parameters;
Print is an output function whose return value is 1 of type int, and only one parameter is supported.
echo usage can be comma "," to connect multiple parameters, you can also use the English dot number "." As a hyphen, make up a parameter, and give a chestnut:
Echo CHR (ten); Echo ' This '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n";
Print cannot use the comma "," to connect multiple parameters, only the English dot number "." form a parameter as a hyphen. For example, the second sentence above is written in print:
Print (' this '. ' String '. ' was '. ' Made '. ' with concatenation. ' "\ n");
second, the focus has come, in PHP , both the sprintf and printf functions are used to format the operation strings, and the output syntax formats are:
- The syntax format for
- printf is: int printf (string
$format
[, mixed $args
[, mixed $ ...
])
- sprintf syntax format is:string sprintf ( string
$format
[, mixed $args
[, mixed $...
]] )
The difference between the two is:
- The printf () function can output the formatted string directly, and the sprintf () function needs to use the Echo method to output the formatted string.
- The return value of the printf () function is of type int, which represents the number of characters printed out of the string, and the return value of the sprintf () function is a string.
For example, we all understand:
echo $str. " <br> "; The output here is a example for this.
$number =print ($str. " <br> "); The output here is a example for this.
echo $number. " <br> "; Output here 1
$num 1=printf ($format, 65,65,65,65); Here output 1000001, A, 65, 65
echo "<br>";
echo $num 1. " <br> "; Here output 18, corresponding to the number of strings
Echo sprintf ($format, 97,97,97,97); Using echo Output 1100001, A, 97, 97
?>
PHP output function Print, printf, sprintf the difference