Shorthand If – The Ternary Operator

You are probably already familiar with shorthand if, also known as the ternary operator. $value = get_value(); $default_value = 10; $selected_value = ($value ? $value : $default_value); If you haven’t seen shorthand if (line 3) before, it is a more elegant Read More …

Verbose If

What is wrong with this if statement? if($condition){ return true; }else{ return false; } It will work, but it is unnecessarily verbose. This way is better: return ($condition); See the difference? We just turned five lines of code into one Read More …

do{}while(0);

PHP supports many useful looping and branching structures, but sometimes a special case calls for a specialized structure Understanding do{}while(0); The do{}while(0); structure can be understood as a do{} structure. The while is set to false which will prevent it from Read More …

What’s the difference between exec(), passthru(), shell_exec(), and system()?

Did you ever wonder what the difference is between exec(), passthru(), shell_exec(), and system()? Why are there so many functions that seem to do the same thing? Let’s start by looking at what php.net has to say about them: exec Execute Read More …

Converting Sample XML to PHP DOMDocument Code

It’s time for you to build an integration and the integration partner uses a large XML document to accept data from you. They have provided a sample of the XML but no pre-built PHP code. Use this XML to PHP DOMDocument code generator Read More …

Clean URLs: An easy working sample

Clean URLs are great! They add a touch of professionalism and elegance to your website and to your code. We are going to build a barebones clean URL sample that you can use in your next project. To begin, let’s take Read More …

Building a Simple Expandable Div

An expandable div is simply a box with hidden content, but it can be revealed by clicking a button. It is great anywhere that you want lots of detailed information to be readily available but not cluttering the page. For Read More …

PHP Constants: How many is too many?

What are PHP constants? A constant allows you to define data that will not change during execution. Think of it like a variable that is always the same. This may not sound very useful at first but I am going Read More …

Building a PHP Profiler Part 3: Database Load

Profiling Database Load Profiling database load is as simple as counting the number of times that a query is run on the database. Last week we added memory reporting to our profiler. This week we will update it to handle database queries and find Read More …

Building a PHP Profiler Part 2: PHP Memory Usage

Last week’s post included a simple PHP profiler class. This week we will expand on it and add in features to report on PHP memory usage within your script. We also add in some percentage reporting to make it easier Read More …