Improved var_dump() with colored output

As developers, we all do some debugging within the code that we are writing at that moment. Usually the first choice is either calling var_dump or print_r depending on what we are used to but it usually ends the same way:

  1. print '<pre>';
  2. var_dump($something);
  3. // or print_r($something);

Usually the <pre> tag is not even included due to us being lazy and we end up viewing the source because the output that we got in Times New Roman is not really that readable.

Of course, there is Xdebug which we can used but not all of us have control over our webservers or desire to install/enable any extensions.

Here is a class, which you will be able to use in all those occasions.

It could as well be considered an improvement over the class mentioned in one of my previous articles however this class does not change the format of the output, only its colors.

Calculate script execution time (PHP class)

Sometimes when writing PHP code that is supposed to run fast we use the script execution calculation functions. Usually it involves (at least for me :) copying the code from example #1 on PHP's microtime function page and pasting it into your script only to do the same all over again the next time.

However, as good as that script is, it is not versatile enough. It does not, for example, allow you to time execution of only some lines or even more - some separated lines.

Let's look at an example:

  1. for ($i = 0; $i < 100; $i++) {
  2.      fx();
  3.      fy();
  4.      fz();
  5. }

What if we needed to time the execution of fy()? It's not impossible to do using the usual measures however it becomes a bit tedious. We would have to have multiple microtime calls, some local variable to store the total time and so on.

Therefore, to make my and hopefully someone else's life easier, I wrote a class for those occasions. It allows you to start, stop, pause and resume timing of specific parts of your code. It would even allow you to calculate total time it takes to run fx() AND fz() together.

Parse INI file into a multi-dimensional array

PHP's native parse_ini_file function allows you to process simple ini configuration files. As the documentation says "parse_ini_file() loads the ini file specified in filename, and returns the settings in it in an associative array.".

However, the problem with the function is that it will only give you an array of key => value pairs, where key will be a string and value will be a mixed value.

What if you wanted your keys themselves to be structures? For those occasions I have written the Ini_Struct class.

This class allows you to:

  • define multi-dimensional structures
  • group configurations (e.g. production, development, testing, etc.) into separate sections (native INI sections)
  • extend sections from one another
  • override keys of extended sections in extending sections

Check if the file is a PNG image file by reading its signature

Here is a quick function to check if a file is a PNG file by reading the file's signature. As this page on Wikipedia states, "A PNG file starts with an 8-byte signature. The hexadecimal byte values are 89 50 4E 47 0D 0A 1A 0A; the decimal values are 137 80 78 71 13 10 26 10".

Knowing that we can compare the first 8 bytes of the file itself to this list. And here is how:

  1. <?php
  3. /**
  4.  * Check if a file is a PNG file. Does not depend on the file's extension
  5.  *
  6.  * @param string $filename Full file path
  7.  * @return boolean|null
  8.  */
  9. function isPngFile($filename)
  10. {
  11.     // check if the file exists
  12.     if (!file_exists($filename)) {
  13.         return null;
  14.     }
  16.     // define the array of first 8 png bytes
  17.     $png_header = array(137, 80, 78, 71, 13, 10, 26, 10);
  19.     // open file for reading
  20.     $f = fopen($filename, 'r');
  22.     // loop through first 8 bytes of the file
  23.     for ($i = 0; $i < 8; $i++) {
  24.         // convert current character to its ascii value
  25.         $byte = ord(fread($f, 1));
  27.         // return false if it doesn't match png's header
  28.         if ($byte !== $png_header[$i]) {
  29.             fclose($f);
  30.             return false;
  31.         }
  32.     }
  33.     fclose($f);
  35.     return true;
  36. }

Compress and output CSS

Have you ever wanted to reduce the size of your CSS files that you serve to your visitors? If so then here is a quick solution to it. This function will remove all unnecessary spaces, tabs, new line characters as well as multi- and single-line comments reducing the size of your CSS file quite significantly.

Here is the function source:

  1. <?php
  3. /**
  4.  * Compress contents of a CSS file
  5.  *
  6.  * @param string $css Contents of a css file
  7.  * @return string
  8.  */
  9. function compressCss($css)
  10. {
  11.     // remove multiline comments, new lines, tabs and single line comments
  12.     $css = preg_replace_callback('/(\/\*.*?\*\/|\n|\t|\/\/.*?\n)/sim',
  13.         create_function(
  14.             '$matches',
  15.             'return "";'
  16.         ), $css);
  18.     // remove all around in ",", ":" and "{"
  19.     $css = preg_replace_callback('/\s?(,|{|:){1}\s?/sim',
  20.         create_function(
  21.             '$matches',
  22.             'return $matches[1];'
  23.         ), $css);
  25.     return $css;
  26. }

Showing 6 - 10 of 27 results