Home Backend Development PHP Problem How to output string in php

How to output string in php

May 31, 2022 pm 05:58 PM
php php string

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles