Var_dump

One of the most popular way of debugging in PHP. still remains the same – showing variables in the browser, with hope lớn find what the error is. Laravel has a specific short helper function for showing variables – dd() – stands for “Dump & 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 khổng lồ cliông xã to lớn expand it – ok, let’s vì chưng that:

Mmilimet, now we know which type we are dealing with – “PaymentMethod”. really helpful…Let’s expvà one of the rows now – maybe we will finally see the values?

Nope. The final step is lớn expand attributes or original sections.

Bạn đang xem: Var_dump

But you get the point – khổng lồ see the actual values, we need khổng lồ cliông chồng three additional times, và 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() và die()

Good old PHPhường way of showing the data of any type:

$methods = PaymentMethod::all();var_dump($methods);die();What we see now:

Not easily readable, is it? The trichồng here is lớn 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 lớn QuickAdminPanel.com

print_r()

Another PHPhường built-in function print_r() has a perfect mô tả tìm kiếm for us: “Prints human-readable information about a variable”

So if we vì chưng this:

$methods = PaymentMethod::all();print_r($methods);die();And then go khổng lồ View Source of the browser… We get this:

Now we can read the contents easily & try to lớn investigate the error.

Xem thêm: Sửa Lỗi Ngữ Pháp Tiếng Anh Thần Thánh, Kiểm Tra Ngữ Pháp Tiếng Anh (Rất Đơn Giản)

Moreover, print_r() function has another optional parameter with true/false values – you can not only emang đến the variable, but return it as string into another variable. Then you can combine several variables inlớn one & maybe log it somewhere, for example.

So, in cases like this, dd() is not that convenient – PHPhường native sầu functions lớn the rescue. But if you want the script khổng lồ literally “dump one simple variable và die” – then dd($var) is probably the faskiểm tra lớn type.