Echo () function definition and usage
The echo () function outputs one or more strings.
Syntax
echo(strings)
Parameters |
Description |
Strings |
Required. One or more strings to be sent to the output. |
Tips and comments
Note: ECHO () is actually not a function, so you do not need to use parentheses for it. However, if you want to pass one or more parameters to echo (), a parsing error occurs when brackets are used.
Tip: The echo () function is a little faster than the print () function.
Tip: The echo () function can use the simplified syntax. See example 5.
Example 1
<?php$str = "Who‘s John Adams?";echo $str;echo "<br />";echo $str."<br />I don‘t know!";?>
Output:
Who‘s John Adam?Who‘s John Adam?I don‘t know!
Example 2
<?phpecho "This textspans multiplelines.";?>
Output:
This text spans multiple lines.
Example 3
<?phpecho ‘This ‘,‘string ‘,‘was ‘,‘made ‘,‘with multiple parameters‘;?>
Output:
This string was made with multiple parameters
Example 4
The difference between single quotes and double quotes. Single quotes only output variable names, not values:
<?php$color = "red";echo "Roses are $color";
echo "<br />";echo ‘Roses are $color‘;
?>
Output:
Roses are redRoses are $color
Example 5
Simplified Syntax:
<?=$color?></p></body>Print () function definition and usage
The print () function outputs one or more strings.
print(strings)
Parameters |
Description |
Strings |
Required. One or more strings sent to the output. |
Tips and comments
Note: The print () function is not actually a function, so you do not need to use parentheses for it.
Note: The print () function is a little slower than echo ().
Example 1
<?php$str = "Who‘s John Adams?";print $str;print "<br />";print $str."<br />I don‘t know!";?>
Output:
Who‘s John Adams?Who‘s John Adams?I don‘t know!
Example 2
<?phpprint "This textspans multiplelines.";?>
Output:
This text spans multiple lines.
Example 3
<?php$color = "red";print "Roses are $color";print "<br />";print ‘Roses are $color‘;?>
Output:
Roses are redRoses are $color
Sprintf () function definition and usage
The sprintf () function writes formatted strings to a variable.
Syntax
sprintf(format,arg1,arg2,arg++)
Parameters |
Description |
Format |
Required. Conversion format. |
Arg1 |
Required. Required to be insertedFormatThe parameter at the first % symbol in the string. |
Arg2 |
Optional. Required to be insertedFormatThe parameter at the second % sign in the string. |
Arg ++ |
Optional. Required to be insertedFormatThe third, fourth, and other % parameters in the string. |
Description
ParametersFormatIs the conversion format, starting from the percent sign ("%") to the end of the conversion character. PossibleFormatValue:
- %-Percentage sign returned
- % B-binary number
- % C-Characters Based on ASCII values
- % D-Signed decimal number
- % E-resumable counting (for example, 1.5e + 3)
- % U-Unsigned decimal number
- % F-floating point number (local settings aware)
- % F-floating point number (not local settings aware)
- % O-octal values
- % S-string
- % X-hexadecimal (lowercase letter)
- % X-hexadecimal (uppercase letters)
Arg1,Arg2,++And other parameters will be inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insertArg1, Insert at the second % symbolArg2, And so on.
Tips and comments
Note: If the % symbol is greaterARGParameter, you must use a placeholder (variable location ). The placeholder is inserted after the % symbol, which consists of numbers and "\ $. See example 3.
Example 1
<?php$str = "Hello";$number = 123;$txt = sprintf("%s world. Day number %u",$str,$number)
;echo $txt;?>
Output:
Hello world. Day number 123
Example 2
<?php$number = 123;$txt = sprintf("%f",$number)
;echo $txt;?>
Output:
123.000000
Example 3
<?php$number = 123;$txt = sprintf("With 2 decimals: %1\$.2f<br />With no decimals: %1\$u",$number)
;echo $txt;?>
Output:
With 2 decimals: 123.00 With no decimals: 123