Logging: A quick and simple way to log output in PHP

If you’ve ever used JavaScript or jQuery, then you’ve probably been spoiled by the built-in console that comes with most modern web browsers. A quick call to console.log() is all it usually takes to quickly inspect your data, providing a fast and invaluable debugging tool while you’re developing your project.

Unfortunately, being a server-side language, PHP isn’t afforded the same luxury, and although plenty of solutions do exist to solve this problem, most of them are often overkill when all you want to do is peek at an array or the contents of a variable during code execution. Debuggers and Unit Testers are available in many IDEs, which are great for complex object-oriented code and when you need to do more than just peek at a value, but more often than not, I find myself using this;

It’s probably one of my most-used code-snippets, because the majority of the time I just want to ensure the data I’m returning from a database query is correct, or that an array I’ve dynamically-built contains the correct data in the right structure. It means I can make alterations quickly, when stepping through a debugger or setting up Unit Tests would take longer. The pre tags ensure the browser preserves spacing and line-breaks, and print_r() provides a nice clean readable format for arrays;

Although this method can be quick, it can also be inconvenient to stop the entire execution of your project. Sometimes however this is unavoidable, especially when working with MVC frameworks, as the final page your browser displays might not be the one you’ve printed your output to. You may wish to see output from your controller or your model, but without stopping execution flow, your application may render your view and prevent you from seeing your test output. Then there’s production code. If you have no other options than to work directly on a live system, then die() is one of the last functions you’d want to use.


A more elegant solution would be to create a helper class that would allow you to log data at any stage during your application.

Then call the static method, passing in your array or variable;

The method will append to the log by default, unless you pass in a different mode. It will generate a html log file in the source directory of the class. You can of course change this if you wish to output your log within a particular directory. Reloading the log is then just a simple matter of hitting refresh in your browser.


While these quick code snippets are by no means a solution to every debugging problem you’ll face, they’re still a useful and time-saving strategy when you need quick access to variable output.

Colourful Random Walks in Processing

A language suited for quickly prototyping your ideas

Processing is a great language for quickly prototyping and designing visual representations of processes. Built on top of Java, it provides a great layer of abstraction that takes away a lot of the verbosity of Java, allowing you to focus purely on creating your sketch (what Processing calls its programs). A quick search online will reveal some wonderful visual eye-candy created within the language, as well as representations of complex data processes, such as fractals and cellular automata, to evolutionary, physics and A.I simulations. Indeed, it’s a language well-suited to scientists and mathematicians just as it is to graphic designers and digital artists.

It’s also supported by a whole host of user-created libraries to extend to the language even further, offering a good level of extensibility in many different areas of programming.


The Sketch

Just as an example of what is possible in a short amount of time with Processing, I decided to experiment with random walks. I wanted to visually represent the walk by changing its colour at every single step, however the colours changed so frequently that it looked like a jumbled mess. It looked more like image noise than any sort of representation of a Random Walk, so that wasn’t going to work.

So, instead of randomly changing colour at every step/iteration, I added in a counter that changed the walker to a random colour value every 5000 iterations;

A random walk in processing

A random walk in Processing using random RGB values.

While this is visually appealing to look at when the walk doesn’t cover too much of the application window (right), it quickly becomes rather garish after many iterations, as is apparent on the left-hand side of the image.

So in the next version of the sketch, I created an array of colours (represented by hex values) that worked well together, and passed that array into my walker object’s constructor. Inside the Walker class, I pick a random index value from the array, and assign the hex value stored at that position in the array to be the colour of the walker every 5000 steps;

A random walk in processing

A random walk in Processing using a random hex value from a predefined set of colours.

Many more iterations later

Even after many iterations, with fewer colours, and by using a complementary colour-set, the effect is much nicer to look at;

A random walk in processing

Many more iterations later.

All of this took surprisingly little code, thanks to Processing. A little under 70 lines of code, including white-space. A great language to learn, whether you’re new to programming, or an experienced programmer.

Because Processing abstracts out a lot of the underlying Java code, it allows you to be much more succinct, without compromising on any of the syntactic sugar that makes Java an easy object-oriented language to work with.

Digital Cityscape – Fractal Art

Digital Cityscape - Fractal art

Digital Cityscape – Fractal art

Created using Ultra Fractal 5.04. You can see a higher quality render of the artwork here or by clicking the image above.

Contains 6 individual layers in total, utilising one fractal formula and three different colouring algorithms. Might experiment with this particular fractal formula again in future, as it has some interesting properties for geometric fractals.