How to use the var_dump() function in php

青灯夜游
Release: 2023-03-16 17:14:01
Original
3996 people have browsed it

In PHP, var_dump() is used to output relevant information about variables, including type, value, etc. The syntax is "var_dump ($expsn)", and the parameter "$expsn" specifies the variable to be output, which can be a A single variable or an expression containing multiple space-separated variables of any type. The var_dump()) function will display structural information about one or more expressions, including the type and value of the expression; if the accepted parameter is an array type, the value will be recursively expanded and its structure will be displayed through indentation.

How to use the var_dump() function in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

PHP var_dump() Function

var_dump() function is used to output information about variables.

Syntax

var_dump ($expsn)
Copy after login
  • $expsn: Specify the variable you want to output. It can be a single variable or an expression containing multiple space-separated variables of any type.

Return value: No return value.

The var_dump() function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values ​​recursively, showing their structure through indentation.

Tip As well as outputting the results directly to the browser, you can use the output control function to capture the output of the current function and then (for example) save it to a string.

Example 1:

<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>
Copy after login

How to use the var_dump() function in php

##Example 2:

<?php
$b = 3.1;
$c = true;
var_dump($b, $c);
?>
Copy after login

How to use the var_dump() function in php

Extended knowledge: print_r() and var_dump() are similar

The print_r() function is used to print variables in a more understandable form, through print_r( ) function can output the contents and structure of the entire array, and will display the keys and elements according to a certain format.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array("姓名"=>"张三","年龄"=>25,"性别"=>"男");
print_r($arr);
?>
Copy after login
Output result:

How to use the var_dump() function in php

The difference between print_r() and var_dump():

Use the print_r() function to print out the entire Array content and structure, display keys and elements according to a certain format. Note that the print_r() function is not just for printing, it is actually used to print easy-to-understand information about the variables.

var_dump() function can also be used to print the data and structure of the array. However, the var_dump() function is more powerful than print_r(). It can print multiple variables at the same time and give the type information of the variables.

var_dump() function displays structural information about one or more expressions, including the expression's type and value. Arrays will expand values ​​recursively, showing their structure through indentation.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to use the var_dump() function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template