PHP

Posts about the server side scripting language PHP

Latest MySQL Errors With PHP

Pinpointing where an error in a program is occurring is not always easy. This can be even more of a problem when you write an SQL query that produces an error. Take the following select statement, which is clearly wrong.

ELECT * FROM table;

I tend to do this at least every now and then, and don't usually spot it until it's too late. So when I then run mysql_query() on this statement nothing happens. As far as I can tell form the code the table contains no data.

To get the error message that is produced from the SQL you can use the mysql_error() function to return the error message. The single optional parameter is the resource identifier for the MySQL connection, but if you leave this out then PHP will use the latest resource created.

echo mysql_error();

This will print the following line in your PHP output.

HTML Checkbox To PHP Array

To create a simple HTML check box use the following bit of code.

<input type="checkbox" name="option2" value="Milk" />

To set the checkbox as filled in include a checked attribute. To make the control XHTML compliant you will need to do the following.

<input type="checkbox" name="option2" value="Milk" checked="checked" />

When the form is posted this value is sent to PHP with the $_POST superglobal array.

To link several checkboxes together to make them into an array in the PHP $_POST array you need to make all of the checkboxes have the same name, and each name must end in "[]".

Creating A 404 Page In PHP

Setting up a 404 page on your site will help users when they navigate to a page that doesn't exist. Rather than dropping them into a scary server message you can give them a nice friendly error page. The first step is to make sure that if the user generates a 404 error they are given a nice page. Add this line to your .htaccess file.

ErrorDocument 404 404.html

Now when a user hits a non existent page they will see you nice error page. However, you sometimes will want to produce a 404 page when the server doesn't give out a 404 error, for example, if you have the following URL.

Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want.

The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.

PHP array_merge() Function Improvement

The array_merge() function in PHP is a handy way of adding one or more arrays together. Here is an example of how to use it.

$array1 = array(3, 21, 12); // set up first array
$array2 = array(63, 1, 9); // set up second array
$array3 = array_merge($array1, $array2); // merge arrays
print_r($array3); // print!

This will print the following.

Array
(
 [0] => 3
 [1] => 21
 [2] => 12
 [3] => 63
 [4] => 1
 [5] => 9
)

The only problem with this function is that it resets any numeric keys, so the following example would produce the wrong result.

Creating A URI Slug With PHP

The use of mod_rewrite on a site can have a powerful effect on search engine positioning, but to do it properly you will need to create a "slug" for each page. A slug is a lowercase alphanumeric version of the page title, with any spaces removed.

To get a slug you will need to use a function to turn a readable page title into a string that can be used as part of a URI.

This function is taken from Bramus and his excellent article about creating a post slug, and it does the job very nicely.

PHP Browser Detection

Retrieving the current user agent using PHP is done via the use of the $_SESSION super global array. The following line of code will print off your user agent.

echo $_SERVER['HTTP_USER_AGENT'];

For Firefox on Windows this user agent will look like this.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

This is all fine, but what about getting more meaningful information, like just the version number. Here is a function that will get the version number (major or minor) from anyone visiting a page with Internet Explorer.

PHP Easter Eggs

There are a couple of hidden features that you can see on just about any PHP driven website. You can get either an image or a list of credits for PHP by appending one of the following strings to the end of the URL.


?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
?=PHPE9568F35-D428-11d2-A769-00AA001ACF42
?=PHPE9568F36-D428-11d2-A769-00AA001ACF42
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

Here is a quick explanation of each code.

The glob() Function In PHP

The glob() function in PHP uses simple pattern matching to find files and directories in a directory and return those file names as an array. It simplifies all of the PHP directory functions, so instead of opening the directory and then reading every file name one by one, you can just use glob() and so it in one function call. Additionally, glob() works very closely with the file system and so it very quick.

Here is a simple example.

$globOut = glob('*');

The $globOut variable now contains a list of all of the files and directories in the root folder. If you want to find all of the files ending with HTML in a directory then use the following.

The function takes another parameter which is a constant that defines some ways of changing the output. Here is a list of the available constants.

Save Browser Output To A File With PHP Output Buffering Functions

The PHP output buffering functions provide a handy way of intercepting the contents of the buffer before it is sent to the browser. The output is whatever is sent to the browser whenever you print something off. PHP allows you to capture this output in a buffer before it is sent to the browser.

Output buffering is controlled by two mechanisms. The first is through the php.ini directive output_buffering, which is usually set to off. It can be turned on by setting this to either on, or the number of bytes that the buffer can take up. When this byte allocation is full the output is sent to the browser.