Tag Archives: code

PHP – Memory Usage Information

By observing the memory usage of your scripts, you may be able optimize your code better.

PHP has a garbage collector and a pretty complex memory manager.

The amount of memory being used by your script. can go up and down during the execution of a script. To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.

echo "Initial: ".memory_get_usage()." bytes \n";
/* prints
Initial: 361400 bytes
*/

// let's use up some memory
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}

// let's remove half of the array
for ($i = 0; $i < 100000; $i++) {
unset($array[$i]);
}

echo "Final: ".memory_get_usage()." bytes \n";
/* prints
Final: 885912 bytes
*/

echo "Peak: ".memory_get_peak_usage()." bytes \n";
/* prints
Peak: 13687072 bytes
*/

PHP – Generating Unique ID’s

There may be situations where you need to generate a unique string. I have seen many people use the md5() function for this, even though it’s not exactly meant for this purpose:


// generate unique string
echo md5(time() . mt_rand(1,1000000));

There is actually a PHP function named uniqid() that is meant to be used for this.

// generate unique string
echo uniqid();
/* prints
4bd64c947233e
*/

// generate another unique string
echo uniqid();
/* prints
4bd67c9412340
*/

You may notice that even though the strings are unique, they seem similar for the first several characters. This is because the generated string is related to the server time. This actually has a nice side effect, as every new generated id comes later in alphabetical order, so they can be sorted.
Read more »

PHP – Basic Whois

Whois services are extremely useful to get basic information about a domain name: owner, creation date, registrar, etc. Using PHP and the whois unix command, it is extremely easy to create a basic whois PHP function. Please note that the whois unix command must be installed on your server for this code to work.

$domains = array('home.pl', 'w3c.org');

function creation_date($domain) {
    $lines = explode("\n", `whois $domain`);
    foreach($lines as $line) {
        if(strpos(strtolower($line), 'created') !== false) {
            return $line;
        }
    }

    return false;
}

foreach($domains as $d) {
    echo creation_date($d) . "\n";
}

Javascript – Include your scripts at the bottom of your HTML files

Not so long ago, it was generally considered good practice to insert your Javascript files within the and tags of your html document.

But browsers read html files from top to bottom, and load external files dynamically. Which mean that inserting scripts within the and tags will make your Javascript load before some of the page content.

In order to always load scripts after the content, Javascript files should always been included at the bottom of your html files, as shown below:

    <script src="myscript.js?" type="text/javascript"></script>
  </body>
</html>

WordPress – Display Total Number of SQL Queries on Your Blog

Queries are evil. They slow down your site as you begin to accumulate more, which can negatively effect the user experience.
In fact, Google agrees too.

The first step to lowering queries is to know how many you actually have. Simply paste the following line of code somewhere on your site. I prefer going after the footer.php file.

<?php if (is_user_logged_in()) { ?>
    <?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php } ?>

If you want this to be publicly shown, simply remove lines 1 and 3.