Home  >  Article  >  Backend Development  >  What are the commonly used methods for printing variables in PHP?

What are the commonly used methods for printing variables in PHP?

青灯夜游
青灯夜游Original
2021-11-12 19:16:526380browse

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

What are the commonly used methods for printing variables in PHP?

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

A common way to print variables:

1. echo()

echo outputs one or more strings, which is a PHP statement. It is not a function, so it has no return value

<?php       $name = &#39;张三&#39;; 
   echo($name); 
?>

2、var_dump()

var_dump() Used to display structural information about one or more expressions, print the type, value, and length of the variable

<?php    $name = &#39;张三&#39;; 
  var_dump($name); 
?>

Output result:

3. print()

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

4. print_r()

print_r() is a function used to print easy-to-understand information about variables.

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

It can be seen from the above that the return value of print_r is Boolean, and the parameters are of mix type. Can be a string, integer, array, object class print_r() displays easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements will be displayed in a certain format. object is similar to an array.

<?php
$a = array (&#39;a&#39; => &#39;apple&#39;, &#39;b&#39; => &#39;banana&#39;, &#39;c&#39; => array (&#39;x&#39;,&#39;y&#39;,&#39;z&#39;));
print_r ($a);
?>

What are the commonly used methods for printing variables in PHP?

5. printf()

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

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):

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

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

  • -:左调整变量值;

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

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

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

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

<?php
$number = 9;
$str = "北京";
printf("在%s有 %u 百万辆自行车。",$str,$number);
?>

What are the commonly used methods for printing variables in PHP?

6、sprintf()

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

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

运行结果如下:

3.14

推荐学习:《PHP视频教程

Format Function Description
%% Return the percent symbol
%b Binary number
%c Character corresponding to the ASCII value
%d Decimal numbers containing signs (negative numbers, 0, positive numbers)
%e Use lowercase scientific notation method (e.g. 1.5e 3)
Octal number
String
Hexadecimal number (lowercase letters)
Hexadecimal number (uppercase letters)

The above is the detailed content of What are the commonly used methods for printing variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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