How7o
  • Home
  • Marketing
    MarketingShow More
    The Beginner’s Guide about Facebook Advertising
    6 Min Read
    64 Creative Marketing Ideas to Boost Your Business
    6 Min Read
  • OS
    OSShow More
    How to force quit frozen apps in Ubuntu
    Force Close an App in Ubuntu (xkill, System Monitor, kill -9)
    4 Min Read
    5 Effective Ways to Increase Market Share Online
    6 Min Read
    Tips Debugging with CMS code Optimization Quick
    6 Min Read
  • Features
    FeaturesShow More
    Step by Step Guide to a Technical SEO Audit
    6 Min Read
    10+ Free Tools to Make Your Own Animated GIFs
    6 Min Read
  • Guide
    GuideShow More
    The Ultimate Guide, Easily Make Videos Tutorials
    6 Min Read
    Tips to Keep Your Cloud Storage Safe and Secure
    6 Min Read
  • Contact
  • Blog
Reading: How to Debug in PHP Like console.log (echo, error_log, WordPress debug.log)
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Categories
    • Marketing
    • OS
    • Features
    • Guide
    • Lifestyle
    • Wellness
    • Healthy
    • Nutrition
  • More Foxiz
    • Blog Index
    • Complaint
    • Sitemap
    • Advertise
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > 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.
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

Your email address will not be published. Required fields are marked *

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
How I Fixed Composer Dependency Errors
How I Fixed Composer Dependency Errors Using the –ignore-platform-reqs Flag (Step-by-Step Guide)
January 12, 2026
Transfer Discourse to a new server
How to Transfer Discourse to a New Server on AlmaLinux (Backup + Restore, Step-by-Step)
January 12, 2026
Installed Discourse on AlmaLinux
How I Installed Discourse on AlmaLinux (Docker Method, Step-by-Step)
January 12, 2026
Installing Docker on AlmaLinux guide
Install Docker on AlmaLinux: Step-by-Step (Docker CE + Compose)
January 12, 2026
Change welcome message on Ubuntu VPS server (MOTD + SSH banner)
Change Welcome Message on Ubuntu VPS (MOTD + SSH Banner)
January 12, 2026

You Might Also Like

Laravel Blade Time Format (HH:MM)
Web Development

How to Show Only Hours and Minutes in Laravel Blade (HH:MM)

3 Min Read
Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
WooCommerce homepage filter to hide out of stock products
Web Development

Hide Out of Stock Products from Homepage in WooCommerce (Keep Them Visible Elsewhere)

5 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
How7o

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

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?