Save Browser Output To A File With PHP Output Buffering Functions

19th March 2008

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.

A Garbage Collection Mechanism In PHP

18th March 2008

Garbage collection is a term for a maintenance function in a class or script that you don't want to run every time the script is run. The main function of the script is to clean up anything that the script has used previously, but is now not important in the general running of the system and can be removed with no ill effects. However, it is important that the garbage collection is not run every time the script is run as it may have a detrimental effect on the speed of the system.

17th March 2008

By far the best resource for finding information about PHP and all of the functions available is from the PHP website. Not only can you view the PHP documentation, but you can also download PHP and many of the extensions like the Smarty template system.

PHP5 Filter Functions Part 2

15th March 2008

Following on from the previous post about the PHP filter functions there are two more filter functions that require some extra explanation. These functions are filter_var_array() and filter_input_array().

They work in much the same way as filter_var() and filter_input() but they accept an array as the input. This enables you to sanitize or validate many different variables at the same time.

PHP5 Filter Functions Part 1

14th March 2008

The filter functions are part of the PECL library and should come as standard on most PHP 5 installs. If they aren't there then ask your server administrator to install them.

The filter functions where created to avoid developers having to write lots of unmaintainable code in order to check the validity of variables and to sanitize these variables once validated. So rather than using many different functions and regular expressions to tell if a value is a number, a boolean or even a URL, you can just use these filter fucntions.

Using JavaScript To Run Form Events

13th March 2008

Creating a form as part of an AJAX control is a common practice, but when you include a submit button within the form the default behaviour of the form is to redirect to the destination supplied in action. To run a form using JavaScript you need to include two things.

In order to overwrite the normal operation of a form you will need to replace the action attribute with some JavaScript code that returns false. This is can be done in the form of come in-line JavaScript code (commonly called a bookmarklet) in the place of any URL in the action attribute of the form.

PHP Page Redirection

12th March 2008

To redirect to a different page using PHP you can use the header() function with the parameter 'Location: ' and the destination of the redirect.

PHP Function To Work Out Average Values In Array

11th March 2008

Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.

Biased Random Value From Array With PHP

10th March 2008

Sometimes you will want to get a random value form an array in a biased random way, that is, you will want certain values to be returned more than others. Here is a function that will generate a single key from an array, with a greater change of a higher value being retrieved.

Discover Auto Increment ID After MySQL Insert With PHP

9th March 2008

Inserting a value into a database with an auto incrementing field is quite common. Once you insert the new row you would expect that you need to do another query to get the newly created ID.

Another option is to use the mysql_insert_id() function to retrieve the ID created by the last insert statement.