PHP - alphabetical order
Information and howto articles for PHP
PHP is a powerful open source server-sided scripting language that can be used on a wide variety of operating systems and webservers, including Apache on UNIX and IIS on Windows. PHP contains numerous extensions which enable you to dynamically create images and flash files, parse xml, connect to remote servers using pop, imap, ftp, http and raw sockets, natively access many database servers including PostgreSql, MySQL and Microsoft SQL Server, compress and decompress files among many other powerful extensions. The Electric Toolbox website is programmed in PHP and uses MySQL as the database backend.
PHP has an excellent manual and online documentation which covers every function for every extension available.
Post sort order: Post Date (Newest First) | Post Date (Oldest First) | Alphabetical | Date Updated
301 redirect with PHP
Posted March 15th, 2008 in PHP
When you change the URL/URI of a page, a permanent redirection should be set up from the old address to the new address to let user-agents know where it can now be found. When using a web browser the redirection is usually seamless; for search engine bots they will now know to stop requesting the old page and instead request the new address in the future. This post looks at how to do this with PHP.
Allowed memory size exhausted when installing a PECL module
Posted March 21st, 2008 in PHP
When attempting to install the timezonedb PECL module on one of the CentOS servers I manage, I got the following error message:
# pecl install timezonedb downloading timezonedb-2007.11.tgz ... Starting to download timezonedb-2007.11.tgz (180,439 bytes) ..........................done: 180,439 bytes Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 92160 bytes) in /usr/share/pear/PEAR/Builder.php on line 313
This post looks at how to install a PECL module a different way.
Correcting the PHP timezone
Posted March 16th, 2008 in PHP
The end of daylight savings in New Zealand has been changed from Sunday March 16th (today) to Sunday April 6th. Last week I looked out how to check the daylight savings dates will be applied correctly on Linux, but now that it's actually the day itself it turns out that while the operating system is using the correct date, PHP is not due to PHP5 maintainin its own internal timezone database. This post looks at how to temporarily correct the solution until it's fixed in the distro's PHP builds.
Create a file with a unique name with PHP
Posted February 28th, 2008 in PHP
The PHP function tempnam() creates a file with a unique name. This can be useful if you need to output some data into a temporary file for one reason or another, and the actual name of the file is not important.
Database error handling with the Zend Framework
Posted May 11th, 2008 in PHP
When running database queries in the past, I have used my own database abstraction libraries, Code Igniter, the PEAR database library, ADODB and ADODB Lite. Each of these easily allows error handling by having a function called in the event of a database error occuring. I have recently started using the Zend Framework, but there doesn't appear to be this capability of automatic error handling. This short post looks at how to capture and handle database errors with the Zend Framework.
Determine whether PHP is being run via HTTP or CLI
Posted March 8th, 2008 in PHP
PHP can be run as a webserver module, as a CGI application or from the command line as a CLI script. There is both a function and a PHP constant which allows you to determine whether PHP is being run via HTTP or CLI. This post looks at the function and constant and the possible values, as determined in the PHP 5.2.5 source code.
Determine whether PHP is being run via HTTP or CLI - Follow Up
Posted March 9th, 2008 in PHP
As a quick follow up to yesterday's post about how to determine whether PHP is being run via HTTP or CLI, I decided to do a quick post about where I found the values in the source code which are returned by the php_sapi_name() function and PHP_SAPI constant.
Displaying PHP APC Cache Information
Posted February 9th, 2008 in PHP
APC is the Alternative PHP Cache, which is a free, open, and robust framework for caching and optimizing PHP intermediate code. I posted about how to install APC on Linux a couple of days ago, and will now look at the apc.php script which comes with APC and shows information about how much of the cache is being used, what files are being cached, the number of times they've been accessed etc.
Errors Compiling PHP with Sablotron
Posted November 30th, 2003 in PHP (Updated May 24th, 2005)
When compiling PHP with Sablotron there are errors which prevent PHP from compiling. This article covers what you need to do to enable PHP to compile correctly with Sabloton.
Formatting Dates with PHP
Posted December 20th, 2007 in PHP
PHP has a really useful date() function for formatting dates from timestamps. When used in conjunction with strtotime() you can do some really quick and easy manipulation and formatting of dates.
Get unique array values with PHP
Posted December 29th, 2007 in PHP
The PHP function array_unique() allows you to create a new array which contains only the unique values from the original array. This post looks at some examples of using the array_unique function.
How to tell if it's a leap year with PHP
Posted April 29th, 2008 in PHP
PHP contains a useful date function which can be used to determine if the current year or a specific year is a leap year. This post looks at how to use the date function to do this.
Install APC for PHP on Linux
Posted February 6th, 2008 in PHP
APC is the Alternative PHP Cache, which is a free, open, and robust framework for caching and optimizing PHP intermediate code. What this means is that APC reads your PHP files, parses them into a more efficient binary format and then caches them in memory so that each request for your PHP files and PHP library files can be fed from the parsed cache. This will generally lead to a speed increase when serving a PHP site, especially one with a lot of library files. This post looks at how to install APC for PHP on Linux. The Linux distribution I used was CentOS 5, but it should be fairly similar for most distros.
Install the MSSQL module for PHP on CentOS 5
Posted November 4th, 2007 in PHP (Updated November 8th, 2007)
Unfortunately there is no pre-built module/extension for MSSQL for PHP in the Yum repositories for CentOS 5, so you need to build it yourself. It's a reasonably simple process, but you do need to have gcc and php-devel packages already installed (and a whole bunch of others which I'll list at the end of this post).
Java error executing Zend Studio
Posted September 28th, 2007 in Applications and PHP (Updated September 28th, 2007)
After an upgrade from openSUSE 10.2 to 10.3 I wasn't able to run Zend Studio 5.5.0. I would click the icon to start it up and nothing would happen; even the splash screen would not appear. This post looks at the Java error the upgrade caused and the fix required to make it work again.
Javascript and CSS file timestamps with PHP
Posted November 1st, 2007 in PHP
Many websites I have worked on have frequently modified CSS style sheets and Javascript library files. In order to prevent Javascript errors or layout and style issues caused by web browsers caching these files, I used to rename the file for each revision, and then modify the name of the CSS or JS file in my PHP header include files. This article looks out how I now do this using the modified timestamp of the file.
MySQL Backups with a Command Line PHP Script
Posted December 9th, 2007 in MySql and PHP
I run a number of web servers with PHP and MySQL and have a PHP command line script that runs on a daily basis to back up MySQL databases using the mysqldump command. It would be possible to do this using a simple bash script as well, and I know I used to use a bash script in the past but for some reason switched it to PHP at some stage.
PHP echo with commas versus concatenation
Posted August 30th, 2007 in PHP
"echo" and "print" are used in PHP for writing strings out to a stream. I have always used concatenation when writing out multiple strings with echo or print, but recently discovered that echo accepts multiple arguments or parameters, comma separated like a regular function call. This post does some benchmarking to see which is faster: echo with commas, echo with concatenation, or print with concatenation.
PHP error Call to undefined function ImageCreateFromPNG
Posted January 9th, 2008 in PHP
If you get the following error message in PHP then it means you need to have the PHP GD extension installed. Obviously the /path/to/file.php will be the actual path and filename of the PHP script the error occured in, and the line number will be the actual line.
Fatal error: Call to undefined function ImageCreateFromPNG() in /path/to/file.php on line 379
You'll also get this error message using other functions such as ImageCreateFromJPEG() if the PHP GD library is not installed.
PHP Error Class 'SoapClient' not found
Posted September 25th, 2007 in Apache and PHP
I've often found that something gets left out or forgotten when moving a website from an old server to a new install. I've just finished migrating one of my customer's sites to a new CentOS 5.0 install with Apache 2.2 and PHP 5.1.6, and there's one single PHP script in the site which uses the SoapClient class.
Naturally I'd forgotten to test this particular function until after the migration was complete, and was wondering why the script had failed. A quick look in the Apache error log file revealed the following error message: PHP Fatal error: "Class 'SoapClient' not found"
PHP Error Class 'XsltProcessor' not found
Posted February 13th, 2008 in PHP
If you get the following error message in PHP then it means you need to have the XSL extension installed. Obviously the /path/to/file.php will be the actual path and filename of the PHP script the error occured in, and the line number will be the actual line. The PHP manual says that "PHP 5 includes the XSL extension by default" but one of my CentOS 5 installations it didn't seem to be; the following post looks at how to install this extension on CentOS 5.
Fatal error: Class 'XsltProcessor' not found in /path/to/file.php on line 138
PHP Magic Constants
Posted December 8th, 2007 in PHP
There are several PHP "magic constants" (or "magical contants") which can be useful for a variety of reasons. These magic constants aren't actually constants at all, but effectively behave like them, although the values change depending on the context.
PHP number_format() always returns 0
Posted September 22nd, 2007 in PHP (Updated September 28th, 2007)
After I inadvertently upgraded from openSUSE 10.2 to openSUSE 10.3RC1, I discovered that phpMyAdmin was showing all my database tables with 0 records in them. When I browsed the tables or ran a query that would return records then it would return them as expected. The screenshot shows a test database. You can see it shows 0 in the "Records" column, although it should show 3349 for the first table and 2 for the second.
PHP PEAR DB Code Completion in Zend Studio
Posted November 2nd, 2007 in PHP
The PEAR DB library provides a database abstraction library for PHP, along with many other useful functions for running database queries. When using the PEAR DB library, if you want to enable code completion for these database functions, you need to add PHPDOC syntax to the top of your function calls.
PHP Register Long Arrays HTTP_POST_VARS
Posted November 7th, 2007 in PHP
I have been in the process of moving my websites from a dedicated machine running Gentoo Linux to a VPS (Virtual Private Server) running CentOS 5. One of the ecommerce websites uses an old install of osCommerce which expects register globals and register_long_arrays on, otherwise things won't work very well.
While testing the site out on the new server, I couldn't work out why some of the form posts were working and some weren't. For example, on some pages I could add items to the shopping cart but on others I couldn't. I also couldn't click the form button to proceed to th checkout or update my basket: whenever I tried it kept showing me the same basket page again.
phpMyAdmin
Posted December 10th, 2003 in MySql and PHP (Updated February 24th, 2004)
phpMyAdmin is a tool written in PHP intended to handle the administration of MySQL using a web browser and is one of the most active projects on Sourceforge.
phpMyAdmin prevent popup window for query editing
Posted April 10th, 2008 in PHP
phpMyAdmin by default opens a popup window when you click the "Edit" link underneath a query. I personally prefer to not have a popup window open and this post looks at how to change the default behaviour.
Process Forking with PHP
Posted March 4th, 2004 in PHP
This article covers howto fork a child process using the PHP command line client and PHP process control functions. Several examples illustrate how to create child processes and make the parent wait until they have finished before continuing processing.
Replacing relative URLs with absolute URLs in PHP
Posted December 6th, 2007 in PHP
I've finally set up an RSS feed for this site and one of the things I needed to sort out was the use of URLs and image tags. All the anchor tags in this site are relative, and so are all the image sources. This isn't going to work in an RSS feed because it would mean none of the links would work, and the images would all be broken. Fortunately this is easy to fix with the use of PHP's string replacement functions.
Sablotron 1.01 Core Dumps with PHP and Apache
Posted February 12th, 2004 in Apache and PHP (Updated May 24th, 2005)
There is a known problem with the Sablotron 1.01 libraries on PHP and Apache which can cause pages to not load and core dumps on the server for some XML files when using Apache and PHP.
Splitting a large Apache log file with PHP
Posted January 4th, 2008 in PHP
A little while back I needed to split a large Apache log file (3.5GB) up into smaller pieces and wanted to break it up into several files, with one for each day of the month. I initially tried using "grep" on the command line but this proved to be too slow, and I needed to run it once for each day and month so it was going to take 90+ times to run, extracting just one day at a time over the course of three months. I ended up writing a quick little PHP script which was able to split the 3.5GB file into 3 files, one for each month, in under 4 minutes.
Triggering errors with PHP
Posted December 13th, 2007 in PHP
It is possible to trigger an error in PHP. This can be useful for debugging purposes, or if there is some condition which occurs in your PHP script which requires a PHP notice, warning or full error which causes the script to halt execution, and which would not be generated by PHP itself.
Using strtotime with PHP
Posted December 21st, 2007 in PHP
The strtotime() function in PHP allows you to convert English text date time strings into UNIX timestamps. It is useful for converting database datetime strings into UNIX timestamps and also creating dates into the future or past based on the current time or relative to a date and time in the future or the past. This post looks at some examples of doing this.
Using the HTTP_REFERER variable with PHP
Posted April 18th, 2008 in PHP
When a web browser moves from one website to another and between pages of a website, it can optionally pass the URL it came from. This is called the HTTP_REFERER, and this post looks at how to use this variable with PHP.
Zend Framework Controller Router example
Posted April 12th, 2008 in PHP
The first MVC framework I used was Code Igniter but I've decided to give the Zend Framwork a go because I like their approach to use what you need, and after some studying of the controllers and view functionality those parts do more or less exactly what I want. There are ways to add additional routes to the controllers but I couldn't work out initially how to inject them into the controller from the Zend Framwork documentation. This post shows the original example from the Zend Framework documentation and then a more full example which I constructed using other sources.

