How to output string in php

青灯夜游
Release: 2023-03-16 09:54:01
Original
4865 people have browsed it

Output method: 1. Use echo() to output one or more strings, the syntax is "echo (string)" or "echo string"; 2. Use die() to output a message, And exit the current script, the syntax is "die (string)"; 3. Use printf() to output the formatted string; 4. print() and so on.

How to output string in php

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

In the Web application, most of the pages are displayed Most of them are text or pictures, and most of them are text. If you want to dynamically output these texts through PHP according to the user's needs, you need to define the texts on the web page as strings, and then output them through PHP's string output function.

PHP provides a variety of string output functions for us to use. Let’s introduce them below. The commonly used string output functions in PHP are as shown in the table below.

Function name Function description
echo() Output string
print() Output one or more strings
die() Output one message and exit the current script
printf() Output formatted string
sprintf() Write the formatted string into a variable

1. echo()

echo() is used to output one or more strings. It is one of the most used functions in PHP because it is more efficient to use. Higher 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.

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

[Example] Use echo to output the specified string.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;php中文网&#39;;
$url = &#39;https://www.php.cn/&#39;;
echo $str;
echo &#39;<br>&#39;;
echo($url);
echo &#39;<br>&#39;;
echo $str.&#39;----&#39;.$url.&#39;<br>&#39;;
?>
Copy after login

The running results are as follows:

How to output string in php

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;);   
$str = &#39;php中文网&#39;;
$url = &#39;https://www.php.cn/&#39;;
print($str);
print &#39;<br>&#39;;
print($url);
?>
Copy after login

How to output string in php

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

How to output string in php

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.

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.

格式 功能描述
%% 返回百分比符号
%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 十六进制数(大写字母)

占位符的 % 于后面的字母之间也可以插入一些附加的内容(例如 %.2f):

  • +:在数字前面加上 + 或 - 来定义数字的正负性。默认地,只有负数做标记,正数不做标记;

  • ':规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用,例如 %'x20s;

  • -:左调整变量值;

  • [0-9]:规定变量值的最小宽度;

  • .[0-9]:规定小数位数或最大字符串长度;

注意:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。

【示例】使用 printf() 函数输出指定的字符串。

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

How to output string in php

5、sprintf()

sprintf() 函数的用法和 printf() 相似,但它并不输出字符串,而是把格式化后的字符串以返回值的形式返回,我们可以使用一个变量来接收 sprintf() 函数的返回值,这样就可以在需要时侯使用这个格式化后的字符串了。示例代码如下所示:

<?php
    $num = 3.1415926;
    $str = sprintf(&#39;%.2f&#39;, $num);
    echo $str;
?>
Copy after login

运行结果如下:

How to output string in php

推荐学习:《PHP视频教程

The above is the detailed content of How to output string in php. 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