var_dump() shows a value's type and contents and is useful during development. Avoid exposing raw debug output on production pages because it can reveal internal data.
<?php
$data = ['id' => 12, 'active' => true];
var_dump($data);
?>
<?php
$var=5;
echo var_dump($var); // Output int(5)
echo "<br>";
$var='5';
echo var_dump($var); // Output string(1) "5"
echo "<br>";
$var=5.8;
echo var_dump($var); // output float(5.8)
echo "<br>";
$var=+5;
echo var_dump($var); // Output int(5)
echo "<br>";
$var=-5;
echo var_dump($var); // Output int(-5)
echo "<br>";
$var='a123';
echo var_dump($var); // output string(4) "a123"
echo "<br>";
$var=True;
echo var_dump($var); // output bool(true)
?>
Output is here
int(5)
string(1) "5"
float(5.8)
int(5)
int(-5)
string(4) "a123"
bool(true)
By using var_dump function we can get all details about the variable used. This function returns nothing and can take more than one variable as input. Let us try some examples.
$a=5;
var_dump($a);
The above code will return like this
int(5)
Now let us try bit different by using one string variable
$a='testing';
var_dump($a);
The output is here
string(7) "testing"
We get all information about the string variable $a in above line. The length of the string is 7 here. Now let us try by using more than one variable.
$a=5.9;
$b='testing';
var_dump($a,$b);
Output is here
float(5.9) string(7) "testing"
We can add one array variable to our input to var_dump function.
$a=5.9;
$b='testing';
$ar=array('testing',' only ', ' at ', ' plus2net');
var_dump($a,$b,$ar);
The output is here
float(5.9) string(7) "testing" array(4) <br>{ [0]=> string(7) "testing" [1]=> string(6) " only " [2]=> string(4) " at " [3]=> string(9) " plus2net" }
$arr = array("name" => "John", "age" => 25);
var_dump($arr);
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
$person = new Person("Alice");
var_dump($person);
$password = "secret";
$userData = ["username" => "john", "password" => $password];
unset($userData['password']); // Remove sensitive data before dumping
var_dump($userData);
Explanation:
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.