Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php prints strings

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how php prints strings. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Printing method: 1, use "echo ($arg)" statement; 2, use "print ($arg)" statement; 3, use "die ($arg)" statement; 4, use "printf ($format,$args)" statement; 5, use "sprintf ($format,$arg)" statement.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

There are many string output functions available in PHP, so let's take a look.

1. Echo ()

Echo (), which is used to print one or more strings, is one of the most used functions in PHP because it is more efficient than other string output functions.

Strictly speaking, echo is not really a function (it is a language structure), so you don't have to use parentheses to indicate parameters, but you can use single or double quotation marks. It is important to note that if you want to pass multiple parameters to echo, you cannot use parentheses, otherwise parsing errors will occur.

The syntax format of echo is as follows:

Echo (string $arg1 [, string $...])

Where $arg1 is the parameter to be output.

In addition, another quick use of echo is that you can use an equal sign directly before the start of the PHP tag (before PHP 5.4.0, short_open_tag must be enabled in the php.ini to be valid) and then populate the variable to be output, as shown below:

[example] use echo to output the specified string.

Welcome to Document!

The running results are as follows:

Welcome to https://www.php.cn/----https://www.php.cn/!

2. Print ()

The function of the print () function is the same as that of echo (). The main difference is that echo can accept multiple parameters and no return value, while print () can only accept one parameter and have a return value. The syntax format of the print () function is as follows:

Print (string $arg)

Where $arg is the string to be output. In addition, the print () function always returns 1.

3. Die ()

The die () function is an alias for the exit () function, which outputs a message and exits the current script in the following syntax format:

Die ([string $status]) die (int $status)

Where $status is the content to be output, and if $status is a string, the function outputs it before exiting. If $status is an integer, this value will be used as the exit status code and will not be printed. The value of the exit status code is between 0 and 254. In addition, the exit status code 255 is reserved by PHP and cannot be used. The status code 0 is used to successfully terminate the program.

[example] use die () to launch the script and output a message.

4. Printf ()

The function printf () is used to output a formatted string, just like a function with the same name in C language. The syntax format of the function is as follows:

Printf (string $format [, mixed $args [, mixed $...]])

Where $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 that are used to set parameters inserted into the $format corresponding to the "%" 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, which is represented by the percentage symbol "%" to the conversion character, as shown in the following table.

Format function description%% returns the percentage sign% b binary number% cASCII value corresponding to the character% d contains the decimal number of the plus or minus sign (negative, 0, Positive number)% e uses lowercase scientific counting (e.g. 1.5e+3)% E uses uppercase scientific counting (e.g. 1.2E+2)% u unsigned decimal number% f floating point number (local setting)% F floating point number (non-local setting)% g shorter% e and% f% o octal number% s string% x hexadecimal number (lowercase) Mother)% X hexadecimal number (uppercase)

Some additional content (for example,% .2f) can also be inserted between the following letters of the placeholder:

+: add + or-before the number to define the positivity and negativity of the number. By default, only negative numbers are marked and positive numbers are not marked.

': specifies what to use as a fill, with spaces by default. It must be used with a width specifier, such as% 'x20s

-: adjust the variable value to the left

[0-9]: specifies the minimum width of the variable value

. [0-9]: specify the number of decimal places or the maximum string length

Note: if you use more than one of the above format values, they must be used in the above order and cannot be disrupted.

[example] use the printf () function to output the specified string.

The running results are as follows:

Welcome to visit the website at: https://www.php.cn/789.000

5. Sprintf ()

The use of the sprintf () function is similar to that of printf (), but instead of outputting a string, it returns the formatted string as a return value. We can use a variable to receive the return value of the sprintf () function so that we can use the formatted string when needed. The sample code is as follows:

The running results are as follows:

3.14 Thank you for your reading! This is the end of the article on "how to print strings in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report