Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

console.log() in PHP

Table of contents:

console.log() in PHP

How to log into the console in PHP

PHP is a server-side scripting language and cannot log into the console just like in JavaScript. PHP code is interpreted and executed by the server, not by your visitors’ web browser.

However, like with console.log() in JavaScript, PHP can print values using the var_dump() function.

Let’s explore in more details, shall we?

var_dump() is the console.log() of PHP

var_dump() is PHP’s workhorse for spitting out a variable’s value.

What does it do exactly? It displays structured information about one or more expressions that include its type and value.

If you var_dump() an object, it will display the class name, the object’s attributes, and their respective values.

Pretty neat, huh?

Let’s look at an example:

var_dump(['Foo', 'Bar', 'Baz']);

The output you’d get from this would be:

array(3) {
string(3) "Foo"
string(3) "Bar"
string(3) "Baz"

While var_dump() does a fine job, it isn’t without its limitations.

Its output can sometimes be hard to read, especially with large, nested arrays or objects.

Luckily, the world of PHP debugging is rich and diverse.

Therefore, let me present you Laravel’s debugging helpers, dump() and dd().

dump() and dd() are the console.log() of Laravel

Laravel, the popular PHP framework, introduces a couple of useful helpers that can make our debugging life a bit easier. They’re called dump() and dd().

The dump() function in Laravel works similarly to var_dump(), but it’s a bit friendlier to the eyes.

It prints the data in a more structured and stylized format, making it easier to read and understand.

Let’s use dump() on the same array we used with var_dump():

dump(['Foo', 'Bar', 'Baz']);

The output is more neatly structured and presented:

array:3 [
0 => "Foo"
1 => "Bar"
2 => "Baz"

Now, let’s talk about Laravel’s dd() function.

“dd” stands for “dump and die.”

It works similarly to dump(), but with a slight twist. After dumping the data, it halts the execution of the script. This is particularly useful when you want to examine a specific part of your code without letting the rest of it run.

Again, let’s use dd() on our example array:

dd(['Foo', 'Bar', 'Baz']);
echo "This will never be executed.";

The output would be the same as dump(), but the script execution will stop immediately after.

There you have it, folks! A journey into the antique world of basic PHP debugging. 👴🏻

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

Print an array with PHP (+ Laravel) Print an array with PHP (+ Laravel)

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP (and even Laravel-specific helpers).

Modified on Jun 24, 2023

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Modified on Nov 23, 2023

How does Laravel work? A crystal clear explanation. How does Laravel work? A crystal clear explanation.

Discover my step by step and simple explanation of how Laravel makes your life easier.

Published on Oct 31, 2023

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here


If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -