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

What are the common methods of printing variables in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what are the common methods of printing variables in php, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Common methods are: 1, use echo (), syntax "echo ($var)"; 2, use var_dump (), syntax "var_dump ($var)"; 3, use print (); 4, use print_r (); 5, use printf (); 6, use sprintf ().

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

Several common ways to print variables:

1. Echo ()

Echo outputs one or more strings. It is a PHP statement, not a function, so it does not return a value.

2. Var_dump ()

Var_dump () is used to display structural information about one or more expressions and to print the type, value, and length of the variable.

3. Print ()

Print () is used to output information about the values of one or more strings or variables. It can only print the values of simple type variables, not arrays and objects (print () is slightly slower than echo ()).

4. Print_r ()

Print_r () is a function that prints easy-to-understand information about variables.

Print_r function prototype: bool print_r (mixed expression [, bool return])

As can be seen from the above, the print_r return value is Boolean, the parameter is mix, and can be a string, a shaping, an array, and the object class print_r () displays easy-to-understand information about a variable. If you give string, integer, or float, the variable value itself is printed. If array is given, the keys and elements will be displayed in a certain format. Object is similar to an array.

5. 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.

6. 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

These are all the contents of the article "what are the common ways to print variables in php?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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