How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)
Web Development

How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)

how7o
By how7o
Last updated: January 12, 2026
6 Min Read
Debug PHP like console.log using error_log and server logs
SHARE

I was working on a PHP project (and a bit of WordPress code) and I kept missing my favorite JavaScript habit: console.log(). In JavaScript, I can dump any variable to the browser console instantly. In PHP… I tried the same mindset and quickly realized something important:

Contents
  • Quick answer: what’s the PHP equivalent of console.log?
  • 1) Debug by printing values on the page (fast but messy)
  • 2) Use error_log() (best “server-side console.log”)
  • 3) Log to a custom file (clean + easy to track)
    • Option A: error_log() to a file
    • Option B: file_put_contents()
  • 4) My favorite: a reusable log helper function (especially for WordPress)
  • 5) WordPress built-in debugging (WP_DEBUG + debug.log)
  • Bonus: “console.log” in the browser from PHP (yes, it’s possible)
  • My personal rule of thumb (what I use in real projects)
  • Final thoughts

PHP runs on the server. JavaScript runs in the browser. So there isn’t a true “console.log equivalent” in PHP the same way there is in JavaScript. But the good news is: you have several solid options depending on what you’re building—and once you use them a few times, debugging becomes way easier.

Quick answer: what’s the PHP equivalent of console.log?

If you just want to “print something to see it,” use:

  • echo (simple values)
  • print_r() (arrays)
  • var_dump() (detailed type + value)

If you want the safer “real debugging” version (especially on servers), use:

  • error_log() (logs to PHP/Apache/Nginx error logs)
  • a custom log file (when you want your own logfile)

PHP debugging steps: echo → error_log → custom log → WordPress debug.log → optional console log

1) Debug by printing values on the page (fast but messy)

This is the closest “quick console.log feeling” if you’re debugging a page request. It works best on local/dev environments.

echo for simple values:

$name = "How7o";
echo $name;

print_r() for arrays (wrap in <pre> so it’s readable):

<pre>
<?php
$data = ["user" => "admin", "role" => "editor"];
print_r($data);
?>
</pre>

var_dump() gives type + value (more detail):

<pre>
<?php
var_dump($data);
?>
</pre>

Warning: Don’t leave these in production pages. Printing sensitive data (tokens, paths, user info) is a real security risk.

2) Use error_log() (best “server-side console.log”)

If I had to recommend just one method for most people, this is it. It doesn’t break your HTML output and it works even when you’re debugging AJAX, cron jobs, background requests, and WordPress hooks.

$message = "Something went wrong!";
error_log($message);

Logging arrays/objects? Convert them to a string first:

error_log(print_r($data, true));

Where does it go? Usually into your web server / PHP error logs. Common places are:

  • /var/log/apache2/error.log (Apache on Ubuntu/Debian)
  • /var/log/nginx/error.log (Nginx)
  • Or wherever your hosting panel stores PHP logs

3) Log to a custom file (clean + easy to track)

Sometimes I don’t want to dig through system logs. I just want my own file like myapp.log. You can do that in two easy ways.

Option A: error_log() to a file

$message = "Debug message";
error_log($message . PHP_EOL, 3, __DIR__ . "/my-debug.log");

Option B: file_put_contents()

file_put_contents(__DIR__ . "/my-debug.log", "Hello log" . PHP_EOL, FILE_APPEND);

Important: make sure the folder is writable by PHP, and don’t store logs in public web directories where anyone can download them.

4) My favorite: a reusable log helper function (especially for WordPress)

When I’m debugging WordPress code, I like having a simple helper I can call anywhere. Here’s a safer, improved version of the “woolog” idea:

function woolog($msg) {
    $file = ABSPATH . 'how7o-debug-' . date("Y-m-d") . '.log';

    if (is_array($msg) || is_object($msg)) {
        $msg = print_r($msg, true);
    }

    $line = "[" . date("H:i:s") . "] " . $msg . PHP_EOL;
    file_put_contents($file, $line, FILE_APPEND);
}

Usage:

woolog("Hook fired!");
woolog($_POST);
woolog($user_object);

This creates daily log files in your WordPress root. If you prefer the safer default location, change it to WP_CONTENT_DIR and keep it away from public access.

5) WordPress built-in debugging (WP_DEBUG + debug.log)

If you’re debugging WordPress specifically, enable logging properly in wp-config.php:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

Then WordPress writes logs to:

wp-content/debug.log

Important: Turn this off after debugging on production sites. Leaving debug mode enabled can expose warnings/notices and performance issues.

Bonus: “console.log” in the browser from PHP (yes, it’s possible)

Sometimes I’m working in a PHP template and I want the value in the browser console. The trick is: PHP must output JavaScript.

<script>
console.log(<?php echo json_encode($data); ?>);
</script>

This is handy for quick debugging on a dev site. Just don’t forget to remove it—because you might accidentally leak data into the client-side console.

My personal rule of thumb (what I use in real projects)

  • Local dev page debugging → var_dump() / print_r() in <pre>
  • Server debugging / background hooks → error_log()
  • WordPress debugging → WP_DEBUG_LOG + helper logger
  • Need it in browser console → echo a tiny <script>console.log(...) temporarily

Final thoughts

PHP doesn’t have a direct console.log() because it runs on the server, but once you get comfortable with error_log() and proper log files, debugging becomes cleaner than printing random values in your HTML.

If you want, share what you’re debugging (plain PHP app, WordPress plugin/theme, Laravel, etc.) and I’ll suggest the best logging setup for that specific use case.

TAGGED:Debuggingerror_logloggingphpprint_rserver-sidevar_dumpwordpresswp_debug

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article How to temporarily disable Imunify360 service for testing (cPanel/WHM) How to Temporarily Disable Imunify360 Service (Safe Testing + Fix 503)
Next Article How to force quit frozen apps in Ubuntu Force Close an App in Ubuntu (xkill, System Monitor, kill -9)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

WooCommerce auto add to cart on visit — template_redirect hook and cart dedup
Web Development

How to Automatically Add a Product to Cart on Visit in WooCommerce

8 Min Read
PHP merge arrays without duplicates — union operator and array_unique
Web Development

How to Combine Two Arrays Without Duplicates in PHP

7 Min Read
Store a PHP array in a MySQL database with JSON
Web Development

How to Store a PHP Array in a MySQL Database

5 Min Read
WordPress custom avatar without a plugin — media uploader writes user meta, get_avatar filter renders the image
Web Development

How to Change a User Profile Picture in WordPress Without a Plugin

10 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?