Member-only story
How to Use console.log()
in PHP
PHP is a server-side language, which means it doesn’t interact with the browser’s console in the same way as JavaScript. Since PHP is processed on the server, it cannot directly log to the browser console. However, like how you use console.log()
in JavaScript, PHP has its own way of displaying information during development—using the var_dump()
function.
Think of var_dump()
as PHP’s console.log()
In PHP, var_dump()
is a powerful tool for inspecting variables. It displays comprehensive information about a variable, including its data type and value.
For instance, when you use var_dump()
on an object, it shows the object’s class, attributes, and their corresponding values. Here's an example:
var_dump(['Foo', 'Bar', 'Baz']);
This produces the following output:
array(3) {
[0]=>
string(3) "Foo"
[1]=>
string(3) "Bar"
[2]=>
string(3) "Baz"
}
While var_dump()
is useful, its output can sometimes be cluttered or difficult to read, especially for large arrays or complex objects. Thankfully, PHP has evolved, and there are better options available for debugging.