Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Useful debug functions for PHP development

Friday, December 12th, 2014 by Servage

phpIt is amazing how much time is spent writing the same snippets of code by developers over and over again to achieve some simple little thing which is used many times. For example during debugging of code, developers often apply the same method of outputting a variable’s value, just to check if everything is right. This process can be done with a few lines of code, and often facilitates a couple of functions together to get the desired debugging information. Over time this has created a need to swiftly be able to do this debugging, without using the default PHP functions like print_r or var_dump. They are too inefficient for the development workflow, because used alone they are not always building comprehensible output suitable for the debugging process.

Debug speed matters

The debugging process for PHP applications can be quite tedious. You often have to reload your page to trigger the same error, and thereby you can actually end up spending a lot of time testing manually with many repetitive steps. Therefore any improvements to this process can be of tremendous value, because saving a few seconds for processes that are repeated thousands of times, will essentially free up many hours in the end. Below are some examples of debugging functions which take the default PHP functionality and make it more efficient.

Improved print_r function

The function below is using the original print_r function, but also adding some more advanced output information. This is really convenient and speeds up development. The function can:

  • Display a variable’s value for debugging immediately.
  • Return the debug output string for later display.
  • Manage to build the display string dependent on running via webserver or command line.
  • Enhance the output string to visualize booleans and null better.

Note how the command line detection is implemented for the Laravel PHP framework, as this function was built for a project using it. However, you can quickly adapt this function to suit any platform you might be using.

function pr($value, $returnString = false) {

  if ($returnString)
  {
    ob_start();
  }
  else if ( App::runningInConsole() )
  {
    echo "\n";
  }
  else
  {
    echo '<pre>';
  }
   
  if (is_null($value))
  {
    echo 'NULL';
  }
  else if ($value === true)
  {
    echo 'TRUE';
  }
  else if ($value === false)
  {
    echo 'FALSE';
  }
  else
  {
    print_r($value);
  }
  
  if ($returnString)
  {
    return ob_get_clean();
  }
  else if ( App::runningInConsole() )
  {
    echo "\n";
  }
  else
  {
    echo '</pre>';
  }
}

Improved var_dump function

The function below is using the original var_dump, but making the output nicer looking in a webserver environment. Otherwise the var_dump output can be quite hard to read, thus inefficient to use. With this method the debug output is displayed nicer and therefore provides a better value to the developer who can work faster.

function vd( $value, $returnString = false ) 
{
  if ($returnString)
  {
    ob_start();
  }
  else if ( !App::runningInConsole() )
  {
    echo '<pre>';
  }
    
  var_dump( $value );
  
  if ($returnString)
  {
    return ob_get_clean();
  }
  else if ( !App::runningInConsole() )
  {
    echo '</pre>';
  }
}

Debug Laravel model attributes

The function below is illustrating how the improved print_r function above can be used to greatly improve the debugging process of Laravel model attributes. The function takes any Laravel model, collection or nested array and turns it into readable information.

function pra($model, $returnString = false) {
  
  $data = array();
  
  if (is_object($model) && get_class($model) === 'Illuminate\Database\Eloquent\Collection')
  {
    foreach ($model as $m)
    {
      $data[] = pra($m, true);
    }
  }
  else if (is_object($model))
  {
    $data = $model->getAttributes();
    $relations = $model->relationsToArray();  
    foreach ($relations as $relation => $values)
    {
      $data[$relation] = $values;
    }
  }
  else
  {
    $data = pr($model, true);
  }
  
  return pr($data, $returnString);
}

Employ the examples above to start your own process of building small process improvements to your debugging workflows.

Useful debug functions for PHP development, 3.8 out of 5 based on 5 ratings
Categories: Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.