Home >Backend Development >PHP Tutorial >Function in php for outputting string
The functions used to output strings in PHP include: echo, print, printf, sprintf, vprintf and vsprintf. echo directly outputs multiple strings, print can only output a single string, and vprintf and vsprintf are used to process formatted output of parameter arrays.
Functions for outputting strings in PHP
PHP provides a variety of functions for outputting strings Functions, the most commonly used of which are:
1. echo
echo
function directly outputs one or more strings to the current output buffer . The syntax is as follows:
<code class="php">echo var1, var2, ..., varn;</code>
2. print
print
function is similar to the echo
function, but can only output one string. The syntax is as follows:
<code class="php">print var;</code>
3. printf
printf
function combines strings and variable data and outputs them in the specified format. Its syntax is as follows:
<code class="php">printf(format, arg1, arg2, ..., argn);</code>
4. sprintf
sprintf
function is similar to printf
function, but it does not output directly string, but returns a formatted string. Its syntax is as follows:
<code class="php">sprintf(format, arg1, arg2, ..., argn);</code>
5. vprintf
vprintf
function is similar to the printf
function, but its parameters are An array of parameters. Its syntax is as follows:
<code class="php">vprintf(format, args);</code>
6. vsprintf
vsprintf
function is similar to the sprintf
function, but it does not output directly String, instead returns a formatted string of the parameter array. The syntax is as follows:
<code class="php">vsprintf(format, args);</code>
Selection Guide
For simple string output, the echo
and print
functions are the best Good choice. For output that requires formatting, the printf
and sprintf
functions are better choices. For formatted output that requires the use of parameter arrays, the vprintf
and vsprintf
functions are necessary.
The above is the detailed content of Function in php for outputting string. For more information, please follow other related articles on the PHP Chinese website!