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:
- for ($i = 0; $i < 100; $i++) {
- fx();
- fy();
- fz();
- }
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:
- <?php
- /**
- * Check if a file is a PNG file. Does not depend on the file's extension
- *
- * @param string $filename Full file path
- * @return boolean|null
- */
- function isPngFile($filename)
- {
- // check if the file exists
- if (!file_exists($filename)) {
- return null;
- }
- // define the array of first 8 png bytes
- $png_header = array(137, 80, 78, 71, 13, 10, 26, 10);
- // open file for reading
- $f = fopen($filename, 'r');
- // loop through first 8 bytes of the file
- for ($i = 0; $i < 8; $i++) {
- // convert current character to its ascii value
- $byte = ord(fread($f, 1));
- // return false if it doesn't match png's header
- if ($byte !== $png_header[$i]) {
- fclose($f);
- return false;
- }
- }
- fclose($f);
- return true;
- }
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:
- <?php
- /**
- * Compress contents of a CSS file
- *
- * @param string $css Contents of a css file
- * @return string
- */
- function compressCss($css)
- {
- // remove multiline comments, new lines, tabs and single line comments
- $css = preg_replace_callback('/(\/\*.*?\*\/|\n|\t|\/\/.*?\n)/sim',
- create_function(
- '$matches',
- 'return "";'
- ), $css);
- // remove all around in ",", ":" and "{"
- $css = preg_replace_callback('/\s?(,|{|:){1}\s?/sim',
- create_function(
- '$matches',
- 'return $matches[1];'
- ), $css);
- return $css;
- }
Improved Zend_Debug::dump() and var_dump() functions
The native Zend_Debug::dump() function is a step forward from the usual approach to dumping variables to debug your code (usually by using var_dump or print_r). However, as always, things can be improved and that is what we will try to do in this article.
