- Using Laravel’s dd (dump and die) function in PHP
- Using dd in PHP
- Echoing: dd() vs var_dump() vs print_r()
- var_dump() and die()
- print_r()
- Var_dump in PHP for Solving Functions in the Language
- Basics to Advanced — Learn It All!
- Syntax:
- Example 1
- Output
- Example 2
- PHP var_dump
- Introduction to the PHP var_dump function
- The dump helper function
- Dump and die using the var_dump() and die() functions
- Summary
Using Laravel’s dd (dump and die) function in PHP
One of the most common ways to debug an error in PHP ecosystem is to dump the variable on the screen, and figure out what is going wrong with it.
The most common way we do it in PHP is via using var_dump function.
var_dump function dumps all the information about the variable or object on your page, but it does not stop the execution of the program further.
Let’s consider an example where if you have an associate array and you want to dump it on the screen.
100, 'Oil'=>10, 'Spark Plugs'=>4 ); var_dump($prices); echo "
I am here";
This is what you get on the screen.
As you can see our code did not die and also the formatting isn;t very nice.
Using dd in PHP
If you are a laravel developer you must be aware of dd() function, dd dumps and die. There is no dd function in php if you try to do dd($prices) in the above example it will throw an error. Call to undefined function dd()
We can however create our own dd function, that will dump the variable/object and then die the script execution.
function dd($data)< echo '
'; die(var_dump($data)); echo ''; >
We have created a new dd function, and wrapped the var_dump around pre tags so that it gives out a nice formatting in the browser and a more readable dump.
'; die(var_dump($variable)); echo '
'; > $prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 ); dd($prices); echo "
I am here";
This is how you see the output, and the php scripts die after dumping. It does not execute the further code.
You can isolate this function in a reusable functions file or a Class.
Echoing: dd() vs var_dump() vs print_r()
One of the most popular way of debugging in PHP still remains the same — showing variables in the browser, with hope to find what the error is. Laravel has a specific short helper function for showing variables — dd() — stands for «Dump and Die», but it’s not always convenient. What are other options? First, what is the problem with dd()? Well, let’s say we want to get all rows from DB table and dump them:
$methods = PaymentMethod::all(); dd($methods);We would see something like this:
Oh, ok, three rows. Now what are the values? We have to click to expand it — ok, let’s do that:
Mmm, now we know which type we are dealing with — «PaymentMethod». [sarcasm]really helpful. [/sarcasm] Let’s expand one of the rows now — maybe we will finally see the values?
Nope. The final step is to expand attributes or original sections. But you get the point — to see the actual values, we need to click three additional times, and we don’t see the full result without those actions. At first I thought — maybe dd() function has some parameters for it? Unfortunately not. So let’s look at other options:
var_dump() and die()
$methods = PaymentMethod::all(); var_dump($methods); die();What we see now:
Not easily readable, is it? The trick here is to View Source in browser, in case of Chrome/Windows it’s CTRL+U:
Better, huh? But there’s even more readable way.
Have you tried our tool to generate Laravel adminpanel without a line of code? Go to QuickAdminPanel.com
print_r()
Another PHP built-in function print_r() has a perfect description for us: «Prints human-readable information about a variable» So if we do this:
$methods = PaymentMethod::all(); print_r($methods); die();
And then go to View Source of the browser. We get this: Now we can read the contents easily and try to investigate the error. Moreover, print_r() function has another optional parameter with true/false values — you can not only echo the variable, but return it as string into another variable. Then you can combine several variables into one and maybe log it somewhere, for example. So, in cases like this, dd() is not that convenient — PHP native functions to the rescue. But if you want the script to literally «dump one simple variable and die» — then dd($var) is probably the fastest to type.
No comments or questions yet.
Var_dump in PHP for Solving Functions in the Language
var_dump in PHP is one of the most popular server-side scripting languages for developing online applications. Its ease of use makes it simple to track problems critical to guarantee a quick code-test-learn feedback loop, no matter what you’re building.
Because var_dump in PHP was created before modern browsers, it didn’t have an easy way to log errors to the browser console, even though the browser is the most common way to interact with websites and web applications. PHP is specifically designed for web application development. Also logging to the console with JavaScript is as easy as this.
In the world of development, debugging is just as vital as coding. When a developer has to verify information about a variable, for example, if a function returns an array, it’s best to double-check the return type and the contents of the returned value. A developer can echo the entire contents, but var_dump in PHP has a function that also checks the datatype.
To dump information about a variable, use the var_dump in PHP. This function provides structured data about the specified variable, including its type and value. Recursively, arrays and objects are examined, with values indented to demonstrate structure. This function works well with expressions as well.
Basics to Advanced — Learn It All!
Syntax:
$expsn is a single-argument function that accepts a single variable or an expression containing several space separated variables of any type.
Return Type: There is no return type for this function.
Example 1
// PHP code to illustrate the working
var_dump(var_dump(2, 2.1, TRUE, array(1, 2, 3, 4)));
Output
Example 2
PHP var_dump
Summary: in this tutorial, you will learn how to use the PHP var_dump() function to dump the information about a variable.
Introduction to the PHP var_dump function
The var_dump() is a built-in function that allows you to dump the information about a variable. The var_dump() function accepts a variable and displays its type and value.
Suppose that you have a variable called $balance with a value of 100 :
$balance = 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 like this:
$balance = 100; var_dump($balance);
Code language: HTML, XML (xml)If you open the page on the web 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 to dump information about two variables $amount and $message :
$balance = 100; $message = 'Insufficient balance'; var_dump($balance); var_dump($message);
Code language: HTML, XML (xml)int(100) string(20) "Insufficient balance"
Code language: JavaScript (javascript)To make the output more intuitive, you can wrap the output of the var_dump() function in a pre tag like this:
$balance = 100; echo '
'; var_dump($balance); echo ''; $message = 'Insufficient balance'; echo '
'; var_dump($message); echo ''; Code language: HTML, XML (xml)
int(100) string(20) "Insufficient balance"
Code language: JavaScript (javascript)The output now is much more readable.
The dump helper function
It’s kind of tedious to always echo the opening
and closingtags when you dump the information about the variable.
To make it easier, you can define a function and 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 like var_dump() . It also allows you to define your own functions. These functions are called user-defined functions. And you’ll learn more about it in the function tutorial.
The following defines a function called d() that accepts a variable. It shows the information about the variable and 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 to it as follows:
$balance = 100; d($amount); $message = 'Insufficient balance'; d($message);
Code language: PHP (php)int(100) string(20) "Insufficient balance"
Code language: JavaScript (javascript)The output is much cleaner now.
Dump and die using the var_dump() and 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 dump the information of a variable and terminate the script immediately. In this case, you can combine the var_dump() function with the die() function as follows:
$message = 'Dump and die example'; echo '
'; var_dump($message); echo ''; die(); echo 'After calling the die function';Code language: HTML, XML (xml)
string(20) "Dump and die example"
Code language: JavaScript (javascript)
- 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 and die example'; dd($message);
Code language: HTML, XML (xml)
In the later tutorial, you will learn how to place the functions in a file and reuse them in any script.
Summary
- Use the var_dump() function to dump the information about a variable.
- Wrap the output 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() and die() functions to dump and die.