Top 3 PHP Tips & Tricks

      No Comments on Top 3 PHP Tips & Tricks

aphp

Increase PHP allowed execution time

With a default install of PHP the scripts have a limited execution time in which they much execute, do their thing, and finish. Quite often particularly in shared hosting environment the default limit just isn’t enough. This is quite often the case if you’re trying to upload large files or process a lot of data at once.

If you haven’t already seen it you will be greeted with the following error message if your PHP script reaches the execution time limit.

Fatal error: Maximum execution time of 60 seconds exceeded in /location/of/php/script.php on line 101

The solution to this problem is usually quite a simple one as PHP lets you increase the maximum script execution time quite easily. There are two ways of doing it, the first of which involved editing the php.ini configuration file. To change the PHP script execution time using the php.ini file simply open it up, do a search for “max_execution_time” and increase whatever the value is set to. The number used should be in seconds.

If however you don’t have access to the php.ini file (most shared hosting companies do not allow this) then you’re still in with a chance. It’s possible to change PHPs maximum execution time inside your scripts and it’s very easy to do.

set_time_limit(120);

The above code snippet will set the PHP maximum execution time to be 120 seconds which should be plenty of time for most scripts. This line of code should be put at the very beginning of your PHP scripts making sure that nothing has been sent to the browser before your script has reached this line (otherwise it won’t work).

Alternatively if you don’t have a specific time limit in mind it is possible to tell your server to run your scripts endlessly with the following.

set_time_limit(0);

Setting the seconds value to be zero means your script has no execution time limit and will therefore just keep going. Try to avoid this if possible though: sometimes it’s good for there to be limits in place as it stops your scripts from using up too many system resources.

Is a number divisible

The following simple PHP function returns true or false depending on whether the given number is wholly divisible. This is particularly useful within loops to determine if the current loop counter is at a specific position (e.g. the last column in a HTML table output loop).

function is_divisible($number, $divideby = 1)
{
	if($number % $divideby == 0)
		return true;
	else
		return false;
}

Here’s an example of the function in use. Let’s say we want to loop through the numbers 1 to 9 inclusive and display it in a 3×3 grid. We can use the above function to do exactly that.

for($i = 1; $i <= 0; $i++)
{
	echo $i;
	if(is_divisble($i, 3))
		echo "
";
}

The result of running the above snippet of code will be that after every 3rd number a line break is inserted making the rest continue on the next line down. It’s a very simple example but this PHP function can be used for all sorts of tasks.

Shared hosting change PHP max execution time

When using a shared host the PHP execution time limit set by your host can sometimes be too low. Many hosts specifically set a low max execution time for any PHP scripts run on their servers to avoid any of their customers scripts slowing down and clogging up the servers resources. If for example one customer on a shared host was to run a poorly designed PHP script it could potentially bring the whole server and every other customers using it to a complete halt.

The workaround which by the way is not supported by all hosts is to set the max PHP execution time using htaccess. Do to so create a file named ‘.htaccess’ in the root of your websites directory with the following contents.

php_value max_execution_time 60

php3

Leave a Reply

Your email address will not be published. Required fields are marked *