A simple way to console log in PHP

A review of the problems of console logging in PHP and a proposed one line of code solution

Perren Consulting
3 min readApr 22, 2021
A laptop with a blue elephant with the php logo
PHP Elephant | credited: Ben | Unsplash

I have been programming in PHP since 1998 and I love it. Sure in those days it was not a fully grown up language, indeed originally PHP stood for Personal Home Page. Over the years it has grown into a fully fledged Object Orientated language. Yet it still lacks an inbuilt way to log outputs to the console.

Complicated solutions to the missing console

While PHP is great, coming from an MS DOS and Unix background, I have always missed an easy way to output debug messages and alike to a console. Sure you can echo things out and printr variables to the UI, but when testing functions you do not always want to engage with the browser. So let’s have a look at some of the possible ways to get a PHP console, before I propose a very simple one line of code solution.

  1. Browser extensions provide a console like experience; Firefox has Firebug and Chrome has Chrome-Logger. However, extensions are a pain to maintain and having to use a particular browser is problematic when testing cross platform compatibility. You also have to learn a whole set of new commands, which is just another learning curve to master.
  2. Open source libraries, like PHPDebugConsole, can be installed, but these seem overkill for such a simple outcome, and again is just another learning curve and library to maintain.
  3. Javascript can receive a JSON passed by PHP and then console.log it to the browser console. This is messy and requires code being implanted into the UI by PHP, not very clean.

All these approaches, give a console like experience, but at the expense of simplicity. I have found over the years that they just do not get used. Perhaps I am lazy, but I suspect other developers will be the same, any friction in easy use and they will be ignored.

The simple solution to the missing console

The simple solution is to use PHP file_put_contents function to generate a plain text file with the required console output. This could be to output an error, a variable name, to see what path a script took or check some part of the code is being reached and working. Indeed, for any purpose you would normally use the console when debugging.

Here is a simple example, that I needed to test a batch email program I was working on today. Emails should only be sent to users without the email invalid flag, this is to capture emails that have previously bounced. There is a User table with the fields email and emailInvaid (which is a MySQL boolean). A simple if statement is used to change the path through the program depending on the emailInvalid field (see below).

PHP Code showing  if statement is used to change the path through the program depending on the emailinvalid field
PHP code before use of console log solution

Now it would be helpful to check the condition is working correctly on the fly without any fuss. A simple use of the PHP file_put_contents function achieves some temporary code to check things on the fly.

PHP code showing use of file_put_contents function
PHP code with console log solution

A quick look in the debugMessage.txt file will show whether the email was sent or not.

Text editor showing debug message: email not sent
DebugMessage.txt file contents

Well that’s it. A simple solution I use everyday when working with PHP. I hope you find it helpful. Please let me know your thoughts and how you deal with the PHP console issue.

--

--