An essential guide to php for solving functions in the language

Summary: in this tutorial, you will learn how to use the PHP var_dump() function lớn dump the information about a variable.

Bạn đang xem: An essential guide to php for solving functions in the language

Introduction to the PHP var_dump function

The var_dump() is a built-in function that allows you to lớn dump the information about a variable. The var_dump() function accepts a variable và displays its type & value.

Suppose that you have a variable called $balance with a value of 100:


Code language: HTML, XML (xml)To display the information of the $balance variable, you place it within parentheses that follow the var_dump function name lượt thích this:


Code language: HTML, XML (xml)If you mở cửa the page on the website browser, you’ll see the following output:


The output shows the value of the variable (100) and its type (int) which stands for integer.

The following shows how lớn dump information about two variables $amount and $message:


Code language: JavaScript (javascript)To make the đầu ra more intuitive, you can wrap the output of the var_dump() function in a pre tag lượt thích this:


$balance = 100;echo "";var_dump($balance);echo "";$message = "Insufficient balance";echo "";var_dump($message);echo "";
Code language: JavaScript (javascript)The output now is much more readable.

The dump helper function

It’s kind of tedious to lớn always echo the opening và closing tags when you dump the information about the variable.

To make it easier, you can define a function & reuse it. For now, you can think that a function is a reusable piece of code that can be referenced by a name. A function may have input and also output.

PHP has many built-in functions lượt thích var_dump(). It also allows you to lớn define your own functions. These functions are called user-defined functions. Và you’ll learn more about it in the function tutorial.

Xem thêm: Hoa Lan Bọ Cạp Cách Trồng Và Chăm Sóc Cây Hoa Lan Bọ Cạp (Trồng Ở Vn)

The following defines a function called d() that accepts a variable. It shows the information about the variable và wraps the output in the tag:


function d($data)echo "";var_dump($data);echo "";
Code language: HTML, XML (xml)To use the d() function, you can pass a variable lớn it as follows:


$balance = 100;d($amount);$message = "Insufficient balance";d($message);
Code language: PHP (php)Output:


int(100)string(20) "Insufficient balance"
Code language: JavaScript (javascript)The output đầu ra is much cleaner now.

Dump và die using the var_dump() & die() functions

The die() function displays a message and terminates the execution of the script:


die($status);
Code language: PHP (php)Sometimes, you want to lớn dump the information of a variable và terminate the script immediately. In this case, you can combine the var_dump() function with the die() function as follows:


$message = "Dump và die example";echo "";var_dump($message);echo "";die();echo "After calling the die function";
Code language: HTML, XML (xml)Output:


string(20) "Dump and die example"
Code language: JavaScript (javascript)How it works

First, dump the information about the $message variable using the var_dump() function.Second, terminate the script immediately by calling the die() function.

Since the die() function terminates the script immediately, the following statement did not execute:


echo "After calling the die function";
Code language: PHP (php)Therefore, you didn’t see the message in the output.

To make the code reusable, you can wrap the code snippet above in a function e.g., dd(). The name dd stands for the dump and die:


function dd($data)echo "";var_dump($data);echo "";die();
Code language: HTML, XML (xml)Now, you can use the dd() function as follows:


// .. Dd function$message = "Dump và die example";dd($message);
Code language: HTML, XML (xml)In the later tutorial, you will learn how khổng lồ place the functions in a file & reuse them in any script.

Summary

Use the var_dump() function khổng lồ dump the information about a variable.Wrap the đầu ra of the var_dump() function in a pre tag to make the output more readable.The die() function terminates the script immediately.Combine var_dump() và die() functions to dump & die.
Did you find this tutorial useful?
Yes No
*

Previously
PHP Constants
Up Next
PHP Data Types
*

tìm kiếm for:

Getting Started


PHP Fundamentals


Data Types


Operators


Control Flow


Functions


Arrays


Sorting Arrays


Advanced Functions


Advanced Array Operations


Forms


PHP Login


File I/O


String Opearations


About obatambeienwasirherbal.com


obatambeienwasirherbal.com helps you learn PHP programming from scratch.

Recent PHP Tutorials


Site Links