Get the First Value in a Array (with and without resetting the array pointer)

php first array element

At first sight this question seems like a trivial one – “how to get the first element’s value in a PHP array?”. Most people would answer with:

  $firstElem = reset($array);

This will work for 90% of the cases, but is not a possible approach every time for the following reason:

reset() rewinds array’s internal pointer to the first element and returns the value of the first array element. from the PHP documentation for reset()

So forget about using current() and next() on that array if you use reset() on it.

To overcome this pitfall, we can use a different approch:

function getFirstElem(array $arr){
    return array_shift(array_values($arr))

$firstElem = getFirstElem($myArray);

This will make a copy of the array’s values and get the first element from that copy. Using this approach your internal pointers to $myArray will not be lost.

(Image credit by “Andre Chinn”)

Posted in Uncategorized

How to Unset/Remove a Cookie Immediately with PHP (Even for the Current Request, Without Having to Refresh the Page)

php unset cookie

Setting cookies is generally done using setcookie(), that’s a known fact. However, unsetting them generally is a bit trickier, since the cookie remains available throughout the current request, if requested via the global variable:


The cookie is fully removed/expired on the next page refresh.

The function below will avoid this behavior. It will still remove the cookie on the next page request, but it will also make it unavailable during the current request too, essentially “expiring”/”removing” it immediately.

function removeCookie($cookieName){
    setcookie($cookieName, '', time() - 3600, '/');

To remove e.g. a cookie named “last_username”, you would call:


Image credit to @Ginny

Posted in Uncategorized

How to Insert a MD5 Password (or the Result of Any Other MySQL Function) Into a Field with “Navicat for MySQL”

MySQL supports MD5() and some other cool functions that do the math for you and save the result in the table field, but what happens when you already have a database and the table in place and you are using “Navicat”, plus you don’t want to fiddle with raw SQL?

Luckily, Navicat has the so called “Raw Mode” when viewing a table’s rows which allows us to insert data using MySQL expressions/functions.

Here are the steps on how to insert a MD5()’ed password in a “users” table using Navicat:
1. Open your desired table with Navicat:

navicat raw 1

2. Choose “View->Raw Mode” from the top menu:

navicat raw 2

3. In your password field, type the expression, whose result you want inserted in that field, e.g.


navicat raw 3

4. Hit “Enter” and “Ctrl+S” to apply the changes. Optionally disable “Raw mode” from the menu if you need to go back to the regular mode.

navicat raw 4

Enjoy the result of the MD5, without using complex raw SQL queries that are prone to typos.

Posted in Uncategorized

How to get a link to the still image of a YouTube video?

YouTube has multiple servers for serving images of the videos, hosted on the site. They are mainly used for load ballancing (so you get the image that is hosted on a server, that is physically closest to you so it loads faster).

However, as a developer or a website owner you may want to programmatically build a link to the thumbnail or still image that YouTube uses for a given video.

What you need is – the ID of the video (e.g. if the video in question is, the ID would be m3JAtvsqfF8). When you have the ID of the video, just replace it in the below URL and open it in your browser.

You should immediately see the full-size thumbnail that YouTube uses across its website (note that scaled down versions are also available; just replace the last zero in the URL with “1.jpg”, “2.jpg” or “hqdefault.jpg”).

Posted in Uncategorized

Counting the Number of Options/Elements in a Dropdown with jQuery

Let’s say you have this (very) simple dropdown:

   United States
   United Kingdom

And one day you decide to figure out how much elements or “option” tags the given “select” has.

It’s easy:

var items = $('#my_dropdown option').length;

The above should immediately display “2” since we have 2 countries in the select dropdown.

Posted in Uncategorized

Find Value of Key in Associative Array

I recently stumbled upon the following issue – I had an associative array with key=>value type and I wanted to get the value of a specific key. I wasn’t able to find a suitable built-in PHP function, so I decided to write something of my own. Feel free to reuse it however you like.

Include this function anywhere in your PHP file:

function find_by_key($searched, $array){
	if(!is_array($array)) return FALSE; // We haven't passed a valid array
	foreach($array as $key=>$value){
		if($key==$searched) return $value; // Match found, return value
	return FALSE; // Nothing was found

Then you can use it by following this example:

$sample_array = array(

echo find_by_key('audi', $sample_array);

This example should output ‘expensive’. I hope you find this useful.

Posted in Uncategorized

Check If Viewed Through Mobile

On many occasions, you may want to check if the current visitor is browsing your site through a mobile browser. There are a lot of solutions out there (classes, libraries), but the simplest option has been working fine for me for years.

Posted in Uncategorized

Remove the WordPress Admin Bar

The WordPress admin bar, introduced in version 3.1 is cool and useful, but sometimes it gets in the way of admins and other logged in users (especially to developers, who are currently working on a website’s header).

Here is a simple PHP snippet that will get rid of the admin bar. You can bring it back by just removing these three lines.


These go in your theme’s functions.php file.

Posted in Uncategorized

PHP Function to Check If a Website is Online

PHPAcademy provided this wonderful 2-part tutorial on how to code a short function to check if a given website is online or down currently. Similar functionality can be seen at

Posted in Uncategorized

How to Convert an Array to an Object?

PHP arrays are cool, but if you are switching to the modern coding standards (OOP) you are probably in need of objects more often.

PHP provides a very easy way to change the type of a variable from an array to an object. It’s done via typecasting.

Here’s an example:


// The actual conversion
$obj = (object) $arr;

echo $obj->name; // Will display John
Posted in Uncategorized