Home >Backend Development >PHP Problem >What are the ways to print strings in php
Print method: 1. Use the "echo($arg)" statement; 2. Use the "print($arg)" statement; 3. Use the "die($arg)" statement; 4. Use "printf ($format,$args)" statement; 5. Use the "sprintf($format,$arg)" statement.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP provides many kinds of string output functions , let’s take a look at it below.
1. echo()
echo() is used to print one or more strings. It is one of the most used functions in PHP because it uses is more efficient than other string output functions.
Strictly speaking, echo is not actually a function (it is a language structure), so it is not necessary to use parentheses to specify parameters. You can also use single quotes or double quotes. It should be noted that if you want to pass multiple parameters to echo, you cannot use parentheses, otherwise a parsing error will occur.
The syntax format of echo is as follows:
echo(string $arg1[, string $...])
Among them, $arg1 is the parameter to be output.
In addition, there is a quick way to use echo, that is, you can use an equal sign directly before the PHP start tag (before PHP 5.4.0, short_open_tag must be enabled in php.ini to be effective) and then after Fill in the variables to be output as follows:
<?= $arg1 ?>
[Example] Use echo to output the specified string.
<?php $str = 'PHP中文网'; $url = 'https://www.php.cn/'; echo $str; echo '<br>'; echo($url); echo '<br>'; echo $str.'----'.$url.'<br>'; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> 欢迎访问 <?= $str ?> ! </body> </html>
The running results are as follows:
PHP中文网 https://www.php.cn/ PHP中文网----https://www.php.cn/ 欢迎访问 PHP中文网 !
2. print()
The function of print() function is the same as echo(), the most important thing is The difference is that echo can accept multiple parameters and has no return value, while print() can only accept one parameter and has a return value. The syntax format of the print() function is as follows:
print(string $arg)
Among them, $arg is to be output String. Also, the print() function always returns 1.
3. die()
die() function is an alias of the exit() function. This function can output a message and exit the current script. Its syntax format is as follows :
die([string $status]) die(int $status)
Among them, $status is the content to be output. If $status is a string, the function will output it before exiting. If $status is an integer, this value is used as the exit status code and is not printed. The exit status code has a value between 0 and 254. Additionally, exit status code 255 is reserved by PHP and cannot be used. Status code 0 is used to terminate the program successfully.
[Example] Use die() to exit the script and output a message.
<?php $url = 'https://www.php.cn/'; fopen($url, 'r') or die('链接打开失败!'); ?>
4. printf()
Function printf() is used to output a formatted string, which is the same as the function of the same name in C language. The syntax format of the function is as follows:
printf(string $format[, mixed $args[, mixed $... ]])
Among them, $format is a required parameter, which is used to set the string and how to format the variables in it; the remaining parameters (such as $args) are optional parameters. Used to set the parameters inserted into $format at the corresponding "%" symbol.
The conversion format used by the first parameter of the printf() function is to replace the uncertain (dynamic) part of the string with a placeholder. The placeholder is converted from the percent symbol "%" to Represented by characters, as shown in the table below.
Format | Function Description |
---|---|
%% | Return the percent symbol |
%b | Binary number |
%c | Character corresponding to the ASCII value |
%d | Decimal numbers containing signs (negative numbers, 0, positive numbers) |
%e | Use lowercase scientific notation method (e.g. 1.5e 3) |
Use uppercase scientific notation (e.g. 1.2E 2) | |
Unsigned decimal number | |
Floating point number (local setting) | |
Floating point number (non-native setting) | |
Shorter %e and %f | |
Shorter %E and %f | |
Octal number | |
String | |
Hexadecimal number (lowercase letters) | |
Hexadecimal number (uppercase letters) |
The above is the detailed content of What are the ways to print strings in php. For more information, please follow other related articles on the PHP Chinese website!