Script memory usage, and displaying with PHP

I recently had a problem where a script written by someone else was stopping. There was not warning, error, or indication at all what the problem was. The script simply died as if the process ended.
After a bit of troubleshooting I found that the script was ending due to memory loss. So here is how I used PHP’s memory_get_usage() to troubleshoot through it.

Here is a chart I used for conversion, to simply display the numbers.

1 Byte = 8 Bit
1 Kilobyte = 1024 Bytes
1 Megabyte = 1048576 Bytes
1 Gigabyte = 1073741824 Bytes

Here is how I used echo to display the memory used in MB’s at different points in the script to troubleshoot.

echo "Memory Usage: " . (memory_get_usage()/1048576) . " MB \n";

Getting differences between dates quickly in PHP or MySQL

I recently needed a way to figure out the difference, in days, between two dates. Here is how I did it.

Using PHP:

$expireDate = "2006-02-07";
$year = substr($expireDate, 0, 4);
$month = substr($expireDate, 5, 2);
$day = substr($expireDate, 8, 2);
$splitExpireDate = (mktime(0, 0, 0, $month, $day, $year));
$today = (mktime(0, 0, 0, date("m"), date("d"), date("Y")));
$difference = (($today) - ($splitExpireDate));
$convertToDays = ($difference/86400);
echo $convertToDays;

Using MySQL:

SELECT (TO_DAYS(expire_date) - TO_DAYS(CURDATE())) AS days_expired FROM tablename;

Zend Studio IDE knowledge nuggets!

I recently realized a few things that I didn’t know about the Zend Studio IDE. As many know I have used the Zend Studio IDE for PHP development for some time. All along I have been using the templates built into the IDE, and never realized that they could be changed since I had no need. My coding standard for years has been the same as the default templates built into the Zend Studio. However, my new employer has a large percentage of coders using Vi who had a different standard in relation to white space than I did, and they were not open to changing their coding standard to meet my Zend Studio preference.

So I had a dilemma. How could I continue to produce speedy PHP using the IDE if I had to type out functions, classes, and “if” statements 100%? (I feel that I save a significant amount of time using the template auto complete shortcuts.) The alternative was to continually edit these constructs after allowing the code completion to insert them.

Continue reading Zend Studio IDE knowledge nuggets!

Using bitmask permissions like Unix in PHP application

Have you ever wanted to give read/write/Delete type of permissions to users or other items within a PHP application?  Why not use Unix-style file permissions (read/write/execute for example)?  A PHP implementation can be used for any kind of permissions in scripts and applications.

Use powers of two (the reasoning behind this will become apparent when you see the decoding function) to define your scheme.  Here’s a sample permission scheme:

define('PERMISSION_READ', 1);
define('PERMISSION_ADD',  2);

Now that your permissions scheme is set you can apply it as needed. Using user permissions as an example lets say you wanted to create a user that has permissions to read and delete a log file. Using the above definitions, you would set the user’s permission to “9”.

Properly decoding that permission and apply it to a user can be easily achieved using the following function:

 * Correct the variables stored in array.
 * @param    integer    $mask Integer of the bit
 * @return    array
function bitMask($mask = 0) {
    if(!is_numeric($mask)) {
        return array();
    $return = array();
    while ($mask > 0) {
        for($i = 0, $n = 0; $i <= $mask; $i = 1 * pow(2, $n), $n++) {
            $end = $i;
        $return[] = $end;
        $mask = $mask - $end;
    return $return;

What this function does is break down the permission ($mask – the bit mask) into its components that are powers of 2 and return them in an array. If you did a print_r() of the above functions return with our example of “9” you would get:

    0 => 1, // READ
    1 => 8  // DELETE

Now that you have your array of permissions, you could use the “in_array” function to check if a user has permission to perform a requested action. Take a look at this sample code:

// ...
$_ARR_permission = bitMask(9);
// ...
if(in_array(PERMISSION_READ, $_ARR_permission)) {
    // [...]
else {
    echo 'Access denied.';

Now you have a simple bitmask permission to use in your apps.