There are three types of output statements in PHP: echo, print and printf. Echo is used to output variable values or strings. The syntax is echo $variable1, $variable2, ..., $variableN;. Print is used to output variable values or strings, and the syntax is print $variable;. printf is used to format output, the syntax is printf($format, $arg1, $arg2, ..., $argN);, where $format is the format string, $arg1, $arg2, ... are output variables.
Commonly used output statement types in PHP
There are three commonly used output statement types in PHP:
echo
Purpose: Used to output the value or string of one or more variables to the screen.
Syntax:
<code class="php">echo $variable1, $variable2, ..., $variableN;</code>
Example:
<code class="php"><?php echo "Hello world!"; echo $name, " is ", $age, " years old."; ?></code>
Use: Similar to echo, used to output the value of a variable or a string to the screen.
Syntax:
<code class="php">print $variable;</code>
Example:
<code class="php"><?php print "Hello world!"; print $name . " is " . $age . " years old."; ?></code>
printf
Use: Used to format output, allowing you to specify the output format and type.
Syntax:
<code class="php">printf($format, $arg1, $arg2, ..., $argN);</code>
Parameters:
Example:
<code class="php"><?php printf("Hello %s, you are %d years old.\n", $name, $age); ?></code>
Output:
<code>Hello John, you are 30 years old.</code>
The above is the detailed content of What are the types of commonly used output statements in PHP?. For more information, please follow other related articles on the PHP Chinese website!