Php 简明教程
PHP var_dump() Function
PHP 中的内置函数之一是 var_dump() 函数。此函数显示结构化信息,例如类型和作为此函数的参数给出的一个或多个表达式的值。
One of the built-in functions in PHP is the var_dump() function. This function displays structured information such as type and the value of one or more expressions given as arguments to this function.
var_dump(mixed $value, mixed ...$values): void
此函数返回输出中对象的全部 public、private 和 protected 属性。数组和对象的转储信息缩进得当,显示递归结构。
This function returns all the public, private and protected properties of the objects in the output. The dump information about arrays and objects is properly indented to show the recursive structure.
对于内置整数、浮点数和布尔变量,var_dump() 函数显示参数变量的类型和值。
For the built-in integer, float and Boolean varibles, the var_dump() function shows the type and value of the argument variable.
Example 1
例如,这是一个整数变量 −
For example, here is an integer variable −
<?php
$x = 10;
var_dump ($x);
?>
转储信息如下 −
The dump information is as follows −
int(10)
Example 2
让我们看看它对浮点数变量的行为 −
Let’s see how it behaves for a float variable −
<?php
$x = 10.25;
var_dump ($x);
?>
var_dump() 函数返回以下 output −
The var_dump() function returns the following output −
float(10.25)
Example 3
如果表达式是布尔值 −
If the expression is a Boolean value −
<?php
$x = true;
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
Example 4
对于字符串变量,var_dump() 函数还返回字符串的长度。
For a string variable, the var_dump() function returns the length of the string also.
<?php
$x = "Hello World";
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
string(11) "Hello World"
这里我们可以使用 <pre> HTML 标记,它显示预格式化文本。<pre> 元素中的文本以固定宽度的字体显示,并且文本保留空格和换行符。
Here we can use the <pre> HTML tag that dislays preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both the spaces and the line breaks.
<?php
echo "<pre>";
$x = "Hello World";
var_dump ($x);
echo "</pre>"
?>
它将生成以下 output −
It will produce the following output −
string(11) "Hello World"
Example 5 - Studying the Array Structure Using var_dump()
var_dump() 函数对于研究数组结构非常有用。在以下示例中,我们有一个数组,其中数组的一个元素是另一个数组。换句话说,我们有嵌套数组的情况。
The var_dump() function is useful to study the array structure. In the following example, we have an array with one of the elements of the array being another array. In other words, we have a nested array situation.
<?php
$x = array("Hello", false, 99.99, array(10, 20,30));
var_dump ($x);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
string(5) "Hello"
[1]=>
bool(false)
[2]=>
float(99.99)
[3]=>
array(3) {
[0]=>
int(10)
[1]=>
int(20)
[2]=>
int(30)
}
}
Example 6
由于在前面的示例中“$x”是一个 indexed array ,所以从“0”开始的索引及其值将被转储。如果数组是 associate array ,则会转储键值对信息。
Since "$x" is an indexed array in the previous example, the index starting with "0" along with its value is dumped. In case the array is an associate array, the key-value pair information is dumped.
<?php
$x = array(
"Hello", false, 99.99,
array(1=>10, 2=>20,3=>30)
);
var_dump($x);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
array(4) {
[0]=>
string(5) "Hello"
[1]=>
bool(false)
[2]=>
float(99.99)
[3]=>
array(3) {
[1]=>
int(10)
[2]=>
int(20)
[3]=>
int(30)
}
}
当你使用 var_dump() 来显示数组值时,不需要使用结束标记“ </pre> ”。
When you use var_dump() to show array value, there is no need of using the end tag " </pre> ".
Example 7
var_dump() 函数还可以显示表示类的对象的特性。在下面的示例中,我们声明了一个 Point 类,其中包含两个私有特性“x”和“y”。该类的构造函数使用传递给它的参数初始化对象“p”。
The var_dump() function can als reveal the properties of an object representing a class. In the following example, we have declared a Point class with two private properties "x" and "y". The class constructor initializes the object "p" with the parameters passed to it.
var_dump() 函数提供有关对象特性及其对应值的信息。
The var_dump() function provides the information about the object properties and their corrcponding values.
<?php
class Point {
private int $x;
private int $y;
public function __construct(int $x, int $y = 0) {
$this->x = $x;
$this->y = $y;
}
}
$p = new Point(4, 5);
var_dump($p)
?>
它将生成以下 output −
It will produce the following output −
object(Point)#1 (2) {
["x":"Point":private]=>
int(4)
["y":"Point":private]=>
int(5)
}
PHP 中有一个类似的内置函数,用于生成转储,名为 get_defined_vars() 。
There is a similar built-in function for producing dump in PHP, named as get_defined_vars().
var_dump(get_defined_vars());
它会将所有已定义的变量转储到浏览器中。
It will dump all the defined variables to the browser.