Laravel print_r(), var_dump() and dd()
Hey dude, today we are going to understand about Laravel print_r, var_dump and dd, all are very helpful in terms of debugging in Laravel.
To figure out the error we always trying to view the variables. With Laravel, we can use dd() ( a combination of the dump and die ) to debug the program.
Why do we need to use var_dump?
Why we need to use Laravel var_dump (), when we have print_r (), dd(), dump() and die() for debugging.
Well, Let’s assume,
We want to know the data type of the variable with value at some point of debugging. Then we can use var_dump($variable) it will return something like this
(int) 5 (string) ‘testcase’
I don’t recommend to use var_dump() to view the Laravel collection. Because we have amazing methods print_r() and dd() for that.
You don’t need to use var_dump() unless you want to see the data type of the variable.
Why do we need to use print_r?
With dd(), We have to expand the object and then we can see a different level of data in the object.
We can use CTRL+Click on windows and CMD+click on mac to expand all data at once.
To avoid this limitation we can use print_r() which give us a nice expanded array.
Even we can use pre HTML tag to prettify the array data.
We can also use toArray() helper function to convert Laravel collection into an array.