Home>Article>Backend Development> Can php directly output strings?
php can directly output strings. PHP provides a variety of string output functions: 1. echo(), which can directly output one or more strings; 2. print(), which can directly output one or more strings; 3. die() or exit( ), you can output a message and exit the current script; 4. printf(), used to output a formatted string; 5. print_r(), used to print variables in a more understandable form; 6. var_dump(), used Structural information about the output variable, including type and value.
The operating environment of this tutorial: windows7 system, PHP7 version, DELL G3 computer
PHP provides a variety of string output functions For our use, let’s introduce the commonly used string output functions in PHP as shown in the table below.
1. echo()
echo() is used to output 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.
[Example] Use echo to output the specified string.
'; $str2 = '你好'; echo $str2; echo '
'; echo $str1.'----'.$str2.'
'; ?>
The running results are as follows:
2. print()
The function and function of print() Same as echo(), the main 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 the string to be output. Also, the print() function always returns 1.
'; print($str2); ?>
3. die()
die() function is an alias of the exit() function, which can output a message and exit the current script, the 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 output a message.
4. printf()
The function printf() is used to output formatted strings, and C language The functions with the same name are used the same way. 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.
[Example] Use the printf() function to output the specified string.
', $str, $url); printf('%0.3f
', $num); ?>
5, print_r()
The print_r() function is used to print variables in a more understandable form, through print_r The () function can output the contents and structure of the entire array, and will display the keys and elements according to a certain format.
print_r() can print strings and arrays. If it is string, integer or float, the variable value itself will be printed.
"); print_r($re2); ?>
If it is an array, the keys and elements will be displayed in a certain format. object is similar to an array.
'apple', 'b' => 'banana', 'c' => array ('x','y','z')); print_r($a); ?>
6. var_dump()
var_dump() function is used to output relevant information about variables.
var_dump() function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values recursively, showing their structure through indentation.
Similar to the print_r() function, the var_dump() function can also output the data content and structure of the entire array. However, var_dump() is more powerful than print_r(). It can print multiple variables at the same time and give the type information of the variables.
The var_dump() function can output relevant information (type and value) of variables. When outputting an array, the array will recursively expand the values and display its structure through indentation.
"张三","年龄"=>25,"性别"=>"男"), array("姓名"=>"李四","年龄"=>21,"性别"=>"男"), array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女") ); var_dump($arr); ?>
扩展知识:
printf() 函数的第一个参数使用的转换格式是将字符串中不确定(动态)的部分使用占位符来替代,占位符是以百分比符号“%”到转换字符来表示的,如下表所示。
格式 | 功能描述 |
---|---|
%% | 返回百分比符号 |
%b | 二进制数 |
%c | ASCII 值对应的字符 |
%d | 包含正负号的十进制数(负数、0、正数) |
%e | 使用小写的科学计数法(例如 1.5e+3) |
%E | 使用大写的科学计数法(例如 1.2E+2) |
%u | 无符号的十进制数 |
%f | 浮点数(本地设置) |
%F | 浮点数(非本地设置) |
%g | 较短的 %e 和 %f |
%G | 较短的 %E 和 %f |
%o | 八进制数 |
%s | 字符串 |
%x | 十六进制数(小写字母) |
%X | 十六进制数(大写字母) |
推荐学习:《PHP视频教程》
The above is the detailed content of Can php directly output strings?. For more information, please follow other related articles on the PHP Chinese website!