Category Archives: PHP

PHP Tip: Quick and Easy Variable Debug Display

When developing or prototyping code, I find that I need a quick dump of information about the code I’m working on. This is not limited to just variables, but also function returns and other resources. I created this tiny function that can handle multiple variables and if the first parameter is the name of an export function it will use that for display.


function DumpVariables() {
   // Get all of our variables
   $Vars = func_get_args();
   $VarCount = count($Vars);

   // Determine dump function
   switch ($Vars[0]) {
      case 'print_r':
      case 'var_export':
         $DumpFunction = array_shift($Vars);

         $DumpFunction = 'var_dump';

   // Make our divider
   $Line = str_repeat('-', 30);

   foreach ($Vars AS $Sequence=>$Var) {
      // Handle the first pass of the data
      if (!isset($Check)) {
         // Test for a shell to determine a web or shell environment
         if (!isset($_SERVER['SHELL'])) {
            print "<pre>\n";
         // Header
         print "Dumping {$VarCount} variables ".date('Y-m-d H:i:s')."\n";
         $Check = true;
      // Variable Header
      print "\n\n {$Line} Variable {$Sequence} {$Line}\n\n";
      $DumpFunction($Var); // Dump the variable
    print "\n\n {$Line} End of Dump {$Line}\n"; // Footer
    exit(0); // Kill the process because we are debugging

// Examples
DumpVariables('print_r', $myVar, get_class_methods($myClass), New MyClass);
DumpVariables($Session, $PostData, $DatabaseRecord->toArray());

PHP syntax checker for Git repository changes

So I was reading this article about writing a php lint checker, that would check your php files for syntax errors before commit. Since I use git, I decided to write something similar. I put this in .git/hooks/pre-commit and made that hook executable. So far it’s working very well.

git status | cut -c 3- | egrep  '^(modified|new file)' | egrep '\.(php|html)$' |awk -F: '{ gsub(/[[:space:]]*/,"", $2); print $2}' |xargs -n1 php -l

In Linux, you can take the output of one command, and make it the input of the next command using the pipe (shift \) character. So let’s break down this command and see how this helps us keep developers from committing php code with errors.
Read more »

PHP 5.3 Deprecation Detector

With the release of PHP 5.2.14, the PHP 5.2.x branch has reached end of support. Also many people in the community pushing very hard for php 5.3 adoption. In a perfect world, this would be a very easy process and all of your code would just roll over and work perfectly. This however is nothing like a perfect world. If you use a managed hosting solution, many are still requiring you use php 5.1 since it is the latest stable release in the RHEL repositories. And if that wasn’t enough, php 5.3 deprecates a decent bit of functionality that many older scripts rely on. You can find a list of deprecated functionality in the php manual.

The functionality still works in PHP 5.3, however it now throws a E_DEPRECATED error. I’ve read many blogs who say the best fix for this is to use the following:

// WRONG !!!!!
error_reporting(E_ALL &~ E_DEPRECATED);

Sadly, this code only hides the messages from you and does nothing to actually help solve the problem. This would be like telling someone who is being chased by your favorite movie monster to just close their eyes and they won’t get hacked to bits! That doesn’t work in the movies, and I assure you it doesn’t work in your production environment either. Luckily while the monster is hacking your friend to bits you can quickly put in place a solution that will help you fix these problems using only MySQL and standard PHP functionality.
Read more »