Can php directly output strings?

青灯夜游
Release: 2023-03-17 16:18:01
Original
4855 people have browsed it

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.

Can php directly output strings?

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 $...])
Copy after login

Among them, $arg1 is the parameter to be output.

[Example] Use echo to output the specified string.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str1 = &#39;hello&#39;;
echo $str1;
echo &#39;<br>&#39;;
$str2 = &#39;你好&#39;;
echo $str2;
echo &#39;<br>&#39;;
echo $str1.&#39;----&#39;.$str2.&#39;<br>&#39;;
?>
Copy after login

The running results are as follows:

Can php directly output strings?

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)
Copy after login

Among them, $arg is the string to be output. Also, the print() function always returns 1.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str1 = &#39;hello&#39;;
$str2 = &#39;你好&#39;;
print($str1);
print &#39;<br>&#39;;
print($str2);
?>
Copy after login

Can php directly output strings?

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)
Copy after login

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.

<?php
    die(&#39;hello!&#39;);
?>
Copy after login

Can php directly output strings?

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 $... ]])
Copy after login

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.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;php中文网&#39;;
$url = &#39;//m.sbmmt.com/&#39;;
$num = 789;
printf(&#39;欢迎访问%s,网站链接为:%s<br>&#39;, $str, $url);
printf(&#39;%0.3f<br>&#39;, $num);
?>
Copy after login

Can php directly output strings?

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.

<?php
    $str = 3.1415926;
    $re1 = sprintf($str);
	$re2 = sprintf(&#39;%.2f&#39;, $str);
    print_r($re1);
	print_r("<br>");
	print_r($re2);
?>
Copy after login

Can php directly output strings?

If it is an array, the keys and elements will be displayed in a certain format. object is similar to an array.

<?php
    $a = array (&#39;a&#39; => &#39;apple&#39;, &#39;b&#39; => &#39;banana&#39;, &#39;c&#39; => array (&#39;x&#39;,&#39;y&#39;,&#39;z&#39;));
    print_r($a);
?>
Copy after login

Can php directly output strings?

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.

<?php
    $num = 3.1415926;
    $str = "3.245";
    var_dump($num);
	var_dump($str);
?>
Copy after login

Can php directly output strings?

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.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array
(
    array("姓名"=>"张三","年龄"=>25,"性别"=>"男"),
    array("姓名"=>"李四","年龄"=>21,"性别"=>"男"),
    array("姓名"=>"娜娜","年龄"=>22,"性别"=>"女")
);
var_dump($arr);
?>
Copy after login

Can php directly output strings?

扩展知识:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template