What are the ways to print strings in php

青灯夜游
Release: 2023-03-13 09:02:02
Original
3750 people have browsed it

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.

What are the ways to print strings in php

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 $...])
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
    $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;;
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    欢迎访问 <?= $str ?> !
</body>
</html>
Copy after login

The running results are as follows:

PHP中文网
https://www.php.cn/
PHP中文网----https://www.php.cn/
欢迎访问 PHP中文网 !
Copy after login

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

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)
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 exit the script and output a message.

<?php
    $url = &#39;https://www.php.cn/&#39;;
    fopen($url, &#39;r&#39;) or die(&#39;链接打开失败!&#39;);
?>
Copy after login

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

##%E Use uppercase scientific notation (e.g. 1.2E 2) %uUnsigned decimal number%fFloating point number (local setting)%FFloating point number (non-native setting)%g Shorter %e and %f%GShorter %E and %f##%o%s%x%X

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

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

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

  • -:左调整变量值;

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

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

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

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

<?php
    $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

运行结果如下:

欢迎访问 PHP中文网,网站链接为:https://www.php.cn/
789.000
Copy after login

5、sprintf()

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

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

运行结果如下:

3.14
Copy after login

推荐学习:《PHP视频教程

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!

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
FormatFunction Description
%%Return the percent symbol
%bBinary number
%cCharacter corresponding to the ASCII value
%dDecimal numbers containing signs (negative numbers, 0, positive numbers)
%eUse lowercase scientific notation method (e.g. 1.5e 3)
Octal number
String
Hexadecimal number (lowercase letters)
Hexadecimal number (uppercase letters)