Benchmark: strtotime() vs DateTime vs getTimestamp in PHP

Recently I wrote about how one can implement date comparison in PHP. In the article I presented two approaches. Firstly, using the strtotime() method and another with the DateTime class.
Then, in the comments on the german division of this blog, it was pointed out that the strtotime() variant is probably faster. Because I wasn’t sure about this, I decided to make a small performance test and share the results with you in this article.
How did I test the performance?

Since the question aims on strtotime() vs. DateTime, the DateTime class is contrary to the strtotime() function. So because DateTime class offers more functionality, I had first to create the DateTime object and then get the timestamp of it, because the strtotime function gives a timestamp as return value and I wanted to compare the time needed to generate the identical output.
As input I have taken a date without a custom time […]

TagKeywordFinder – a free WordPress SEO plugin

Last while browsing I came across a WordPress plugin called wpSuggest. This links Google’s autocomplete function (the thing called “Google Suggest”, which ensures that you get suggestions while typing your question) with the title field in the WordPress editor. So if you write an article in the WordPress backend and type the title wpSuggest will make suggestions for the perfect title.
I liked the way the Google Suggest feature was used here. However, I found that the combination of Google Suggest and the input field for the keywords/tags would make much more sense.  So in short I have developed a small WordPress plugin named “TagKeywordFinder”. This plugin works similar to the above mentioned wpSuggest, with the small difference that it is used when tagging the article. And this is how it looks like:

(If you’re not intrested in the setup process, skip it to 1:00.)
When you begin to assign tags for your […]

How to make simple syntax hightlighter in PHP

PHP SyntaxhighlighterIn the following article I will show you how you can write your own syntax highlighter with a few lines of PHP. By use of the following PHP snippet you can easily display PHP code on your homepage.
The whole thing is much more easily than you think. Most of the work is done by an internal PHP function named highlight_file. This function reads a file and formats the source code in color. We then merely have to count the lines of the file and output the line numbers, as well as preformat the font.
Since the snippet is not that long, I think the comments in the snippet itself are sufficient. If you still have any questions, just write a comment.

<?php
function highlight_sourcecode($file)
{
//Count the lines of the input file
$amount_of_lines = count(file($file));

//Create a list with all line numbers
$list_linenumbers = range(1, $amount_of_lines);

//Format line numbers […]

PHP: rand() vs. mt_rand() – what is more accurate, what is faster?

After I was pointed out, that it would be better to use the PHP function mt_rand() instead of rand() for generating random numbers (because mt_rand() should be more accurate), I decided to investigate on that statement. But at first, where is the difference between rand() and mt_rand()?
By use of mt_rand() you can generate random numbers just like with the rand() function. The function call also looks equal on both functions. Though the PHP documentation says mt_rand() is up to 4-times faster and would be create more random/arbitrary numbers. By implication this would mean, that the rand() function creates inaccurate random numbers.
I was not aware of this problem, but after some googleing I discovered that there are a lot of people who think that mt_rand() is the better function. However I had to notice that most of the contributions which complain that rand() is not clean are a “little older”.
Often cited […]

Generate array of different/unique random numbers in PHP

Sometime it is necessary to generate a list of different random numbers. A possible usage scenario therefore could be a raffle where the winners should be choosen by chance, but every single user shouldn’t get more than one prize.
So in the following snippet, I’ll show you how to generate a list of different randoms in PHP.

<?php
//Create an array of numbers from which the randoms
//should be choosen. (For raffle: the list of user id’s)
$array = range($minimum, $maximum);

//Initialize the random generator
srand ((double)microtime()*1000000);

//A for-loop which selects every run a different random number
for($x = 0; $x < $numberOfRandoms; $x++)
{
//Generate a random number
$i = rand(1, count($array))-1;

//Take the random number as index for the array
$result[] = $array[$i];

//The chosen number will be removed from the array
//so it […]