PHP
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
Using PHP's glob() function to find files in a directory
Posted July 2nd, 2009 in PHP
A couple of weeks ago I posted how to read through a directory with PHP using the opendir() readdir() and closedir() functions and now look at the glob() function. glob() returns the filenames into an array and supports pattern matching so it can be very easy to find e.g. jpg images in a particular directory.
Sending a username and password with PHP file_get_contents()
Posted June 29th, 2009 in PHP
Last week I looked at how send a username and password with PHP CURL. Using CURL is useful because you can examine the return information to see if the request was successful etc, but if your hosting provider doesn't have CURL for PHP enabled then you can still attempt to get the file by using file_get_contents and passing the authentication as part of the $context parameter.
Prevent a view rendering with the Zend Framework
Posted June 25th, 2009 in PHP
The front controller of the Zend Framework will render a view by default but it's possible to disable this automatic rendering should you need to, for example if making an Ajax call which sends some JSON data instead of an HTML template.
Sending a username and password with PHP CURL
Posted June 22nd, 2009 in PHP
CURL and PHP combined can be really useful for getting data from websites, connecting to APIs (such as the Google Analytics API) and so on. Sometimes you may need to connect to a website that is password protected so this post looks at how to pass the username and password with PHP and CURL.
Reading through a directory with PHP
Posted June 18th, 2009 in PHP
PHP's opendir() readdir() and closedir() are used for reading the contents of a directory (there's also glob() and scandir() but I'll cover those in later posts). Combined with functions like is_file() is_dir() and is_link() and you can easily build up a directory tree or process files etc.
Autoloading with PHP's SPL library
Posted June 15th, 2009 in PHP
I read Rafael Dohms's post " SPL: a hidden gem " a few days ago and learned of a better alternative to PHP's __autoload function using SPL instead and thought I'd share this here along with some more details about the spl_autoload_register() and spl_autoload_functions() functions.
Google Analytics API PHP Class data method parameters
Posted June 12th, 2009 in PHP
This post documents the parameters that can be passed to the data() method of my PHP class for interfacing with the Google Analytics API. You can read more about my class here as well as download it along with an example script.
PHP's trim() function
Posted June 11th, 2009 in PHP
PHP's trim() function has a second parameter which allows a list of characters to trim to be passed in. This allows characters other than (or instead of) the default space character to be trimmed from a string. It works for trim(), rtrim() and ltrim().
Calculating a page's bounce rate with the Google Analytics API
Posted June 11th, 2009 in PHP
I had an email a couple of days ago from someone asking me how to calculate the bounce rate for a page using my PHP Class for the Google Analytics API.
Paging through result data from the Google Analytics API PHP Class
Posted June 8th, 2009 in PHP
I had a query the other day about paging through Google Analytics API data using my PHP Class; Google allows a maximum of 1000 records to be returned at a time per request so you need to loop until there's no more data, requesting 1000 records at a time starting from offset 1001, 2001 etc.
Get the included files with PHP
Posted June 4th, 2009 in PHP
PHP's get_included_files() function returns an array containing a list of included files by your script, including the script itself. This post looks at the filenames returned in the array and corrects a couple of errors in the PHP documentation.
Articles about how to use PHP's IMAP Functions
Posted June 1st, 2009 in PHP
This is a summary post with links through to all the posts on my blog where I've looked at various functions using PHP's IMAP functions to download mail and extract attachments etc.
List of PHP email libraries
Posted June 1st, 2009 in PHP
I'm going to write a series about how to send and receive email using various different PHP email libraries to show the differences between them and what's required for each to work. This post is a list of PHP mail libraries that I know of and will be reviewing/writing code for over the coming weeks. If you know of any others you'd like me to look at please send me an email and I'll add them to the list.
Using filters with the Google Analytics API and PHP
Posted May 31st, 2009 in PHP
The Google Analytics API allows filtering to be done on the result data before it is fetched so you could, for example, only get data for visitors from the United States. This post shows how to format the filters field in PHP and also how to use my Google Analytics API PHP Class with filters.
Google Analytics API PHP Class - Get most popular pages
Posted May 30th, 2009 in PHP
The following shows example usage to get the most popular pages from Google Analytics using my PHP class for the GA API.
Google Analytics API PHP Class - Updates May 28th 2009
Posted May 28th, 2009 in PHP
I've published an update to my Google Analytics API PHP Class today which adds better support for filtering, and have added some filtering examples and more documentation to the example.php file.
Set PHP configuration options in an Apache virtualhost
Posted May 28th, 2009 in PHP
I've previously looked at how to set PHP configuration options in an Apache .htaccess file which allows you to change PHP_INI_PERDIR or PHP_INI_ALL settings but notPHP_INI_SYSTEM. All three of these settings types can be modified in an Apache virtualhost's settings.
Show PHP configuration options for a selected extension
Posted May 25th, 2009 in PHP
Following up an earlier post about PHP's ini_get_all function to show PHP's configuration options, this post shows how to get just the config for a particular extension.
Google Analytics API PHP Class - Updates May 22nd 2009
Posted May 22nd, 2009 in PHP
I've made some updates to my Google Analytics API PHP Class today and posted the updated class and example files. The changes are documented here.
Rounding numbers with PHP
Posted May 21st, 2009 in PHP
I have previously looked at rounding numbers with MySQL and Javascript and in this post look at rounding numbers with PHP using the round() ceil() floor() number_format() and s/printf() functions.
Google Analytics API PHP Class - Q&A May 21st 2009
Posted May 21st, 2009 in PHP
I've had some more questions about my Google Analytics API PHP Class by email so am posting the information relating to these questions here to help other people out who may have the same issues.
PHP's ini_get_all function
Posted May 18th, 2009 in PHP
PHP has a large number of configuration options which can be set in the php.ini file, Apache virtualhost settings, .htaccess and with the ini_set function. This post looks at the ini_get_all function which returns an array containing all the possible configuration options, what their global and local script value are and where they can be modified.
Who's using my Google Analytics API PHP Class?
Posted May 17th, 2009 in PHP (Updated June 19th, 2009)
I've had a lot of feedback and responses to my Google Analytics API PHP Class and decided to add a page here to list people who are using it. If you are using my class and would like to be listed here then please send me an email and I will add you.
Log PHP errors with log_errors and error_log
Posted May 14th, 2009 in PHP
PHP errors are by default logged to the web server's error log file (at least they are with the default setup on most Linux distros) but it is also possible to instead log PHP errors to a file of your choice. This is useful on production websites so you can periodically check for errors and fix them in a location other than the default webserver error log file.
Google Analytics API PHP Class - Q&A May 13th 2009
Posted May 13th, 2009 in PHP (Updated May 15th, 2009)
I had some questions in an email about my Google Analytics API PHP class last night and have posted them here to help other people too.
Automatically append or prepend files in a PHP script
Posted May 11th, 2009 in PHP
It is possible with PHP to automatically append or prepend a file to a script using the auto_append_file and auto_prepend_file configuration options. These options can be set in the PHP config, virtualhost settings or in a .htaccess file. This post shows how to set these and how to override a setting so no file is appended or prepended.
Looping through the Google Analytics API PHP class data
Posted May 5th, 2009 in PHP
Thanks for Jim Snyder from www.net-netanalytics.com for pointing out my examples were lacking the information to show how to loop through the resulting data from my Google Analytics API PHP Class. I have now update the examples file and also include the information about how to loop through the data in this post. You can always download themost recent version of my PHP class here.
Google Analytics PHP API: Getting data without a dimension
Posted May 4th, 2009 in PHP
My Google Analytics API PHP class was initially developed assuming that a dimension would always be supplied but it doesn't actually need to be, so I modified it to deal with no dimension and present an example here.
Get the Google Analytics profile id from the accounts list
Posted April 30th, 2009 in PHP
Just a quick post about an easy way to get the profile id for a Google Analytics account using my PHP Class. The ->load_accounts() function loads the accounts into an associative array which you can then access from ->accounts. The profile id is stored in there.
Google Analytics API PHP Class Update April 30th 2009
Posted April 30th, 2009 in PHP
Thanks to the several people who emailed me with questions etc about my PHP class for accessing the Google Analytics API. The most common issue was when making a request for data and nothing came back. I've addressed this issue by adding some error triggering in the code and also a note in the example about the correct formatting for the profile id.
Can't connect to the Google Analytics API
Posted April 29th, 2009 in PHP (Updated April 29th, 2009)
I had an email this morning about my Google Analytics API PHP class which said "I'm trying to get your google API class working in my application, it's failing at logon at the moment" so I had a look at the possible reasons for failure and present these here.
The Google Analytics API and PHP: A series
Posted April 28th, 2009 in PHP (Updated June 11th, 2009)
The Google Analytics API is a powerful tool for accessing analytics data without having to log into the web interface and means you can easily have automated processes for collating data and saving it to a database, emailing information on a daily basis etc.
I have created a PHP class for accessing the Google Analytics API with CURL and using DOMDocument to parse the XML. The resulting class and example script can be downloaded here and there are a number of posts on this website showing how to use it.
Converting Google Analytics API XML data into an array with PHP
Posted April 28th, 2009 in PHP
This post updates my Google Analytics API PHP class again with a new method which can be used to get data from any dimension and metric and return it as an associative array. The full class, which I will continue to update over the next few days, can be downloaded here.
Load the Google Analytics API accounts list with PHP
Posted April 27th, 2009 in PHP (Updated April 28th, 2009)
After Google made an API available for Google Analytics the other day I started a series of posts about how to get analytics data with PHP and CURL. Today's post extends the class I've already posted by adding a function to get the list of accounts you have access to. This enables you to get the ID needed to pass to further API calls.
PHP class for using the Google Analytics API
Posted April 23rd, 2009 in PHP (Updated April 28th, 2009)
Just quickly following up my post from earlier on today where I looked at how to log into the Google Analytics API using PHP and CURL using Username/Password Authentication I wrapped the login code up into a class with methods for logging in and then calling API functions.
Log into the Google Analytics API using PHP and CURL using Username/Password Authentication
Posted April 23rd, 2009 in PHP (Updated April 28th, 2009)
Google have finally made an API available for Google Analytics although it's currently in beta and the specs are subject to change. I've been playing around this morning logging in usingClientLogin Username/Password Authentication with PHP and CURL and show how to do this here. In future posts I'll look at how to actually use the API but this will get us all started for now.
Google Analytics API Launched
Posted April 23rd, 2009 in Miscellaneous Postings and PHP
Google have finally launched an API for Google Analytics, rendering my tutorial series about how to get Analytics data via email somewhat redundant. However despite that I'm pleased they've done this as it makes accessing data a lot easier.
PHP's getdate() function
Posted April 20th, 2009 in PHP
I've been coding PHP since 1999 and I'm still discovering functions I didn't know about. Just the other day I read someone's post about some useful PHP functions and I knew all of them except for the getdate() function so I thought I'd cover it here.
Fix the "call to undefined function curl_init" error in PHP on Debian
Posted April 17th, 2009 in Linux/Unix/BSD and PHP
If you call the curl_init function in PHP and do not have the curl module installed you will get the error message "Fatal error: Call to undefined function curl_init() in /path/to/script.php on line XY". This post shows how to install the PHP curl module on Debian Linux and the instructions should also work on all Debian derived distros such as the Ubuntu family.
Set PHP configuration options with an Apache .htaccess file
Posted April 16th, 2009 in Apache and PHP (Updated May 28th, 2009)
PHP has a large number of configuration option which can be set in the php.ini file, Apache <virtualhost> blocks, .htaccess files and ini_set(). This post looks at how to set PHP configuration options with Apache's .htaccess files.
Sending a CSV file to the web browser with PHP
Posted April 13th, 2009 in PHP
In the past I've written about how to save data as a CSV file using PHP, MySQL queries and mysqldump and in this post show how to send the results of this data to a web browser setting the headers correctly with PHP so the browser gives you a "save as" dialog instead of displaying it on the page. The headers also include the filename which is supplied as the default in the save as dialog.
Create a CSV file from MySQL with PHP
Posted April 12th, 2009 in MySql and PHP
There are a couple of ways to export data from MySQL to a CSV file (refer to my using mysqldump to save data to CSV files and export data to CSV from MySQL posts for details) but neither of them supports adding a header row to the CSV which contains the column names. This post looks at how to export the data from MySQL into a CSV file with PHP including a header row.
Fix the "call to undefined function ImageCreateFromPNG" error in PHP on Debian
Posted April 10th, 2009 in Linux/Unix/BSD and PHP
If you call the ImageCreateFromPNG function (and similar image processing functions) in PHP and do not have the GD module installed you will get the error message "Fatal error: Call to undefined function ImageCreateFromPNG()" error message. This post shows how to install the PHP GD module on Debian Linux and the instructions should also work on all Debian derived distros such as the Ubuntu family.
Listing MySQL tables with PHP
Posted April 9th, 2009 in MySql and PHP
This post shows how to use the MySQL "SHOW TABLES FROM" SQL query to get a list of tables using PHP. This list could either be stored to an array or echoed out to web browser, command line etc.
Get a list of IP addresses for a domain with PHP
Posted April 6th, 2009 in PHP
On Thursday I looked at how to get a domain name's IP address with PHP using the gethostbyname() function. However some domains resolve to multiple IP addresses so this followup post looks at how to get a list of IP addresses for a domain in PHP using the gethostbynamel() function.
Apache does not log HTTP status set with PHP header when using mod_rewrite in .htaccess files
Posted April 5th, 2009 in Apache and PHP (Updated April 6th, 2009)
I discovered something rather annoying the other day relating to setting HTTP response codes with PHP's header() function, Apache's mod_rewrite and associated webserver logging. If anyone knows of a solution to the issue I have discovered below I'd love to know it.
Get a domain name's IP address with PHP
Posted April 2nd, 2009 in PHP
It's easy to get the IP address for a domain name using PHP's gethostbyname() and gethostbynamel() functions. This post looks at some examples of using thegethostbyname() function in PHP to get a single IP address for a hostname.
Find unread messages using PHP IMAP
Posted March 30th, 2009 in PHP
Having reached the conclusion of my series on getting Google Analytics data by email I had an enquiry from someone asking how to find only the unread email messages using PHP IMAP. This is simple enough to do and can be done either when looping through the messages or using the imap_search function.
Deleting messages with PHP IMAP and Gmail
Posted March 26th, 2009 in PHP
My last PHP post looked at how to delete email messages using PHP's IMAP functions. This is straight forward on a regular POP3 or IMAP mail server but deleting a message when connected to Gmail just removes it from the inbox and it's still available in "All Mail" rather than being moved into the trash. This post looks at how to move an email into Gmail's trash with PHP IMAP.
Install APC for PHP on Debian 5 Lenny
Posted March 26th, 2009 in Linux/Unix/BSD and PHP
I have recently moved from hosting on a CentOS box to Debian and needed to install PHP's APC opcode cache on it. I've posted about how to do this before on a CentOS box which is a very manual process and now add this post to install APC on Debian which is much easier.
Deleting messages with PHP IMAP
Posted March 23rd, 2009 in PHP
I've been writing a series about sending Google Analytics data by email and then processing the emails with PHP's IMAP functions. Once the email has been processed you'll probably want to delete the emails from the mailbox afterwards so this email looks at how to delete emails using PHP's IMAP functions.
Parsing Google Analytics data with PHP - XML files
Posted March 22nd, 2009 in PHP
This post shows how to extract data from a XML email attachment from data sent by Google Analytics with PHP. Previously I have covered how to get the data from CSV files and TSV files and now it's time for dealing with XML.
Force reload of updated CSS and Javascript files with unique filenames
Posted March 20th, 2009 in Apache, HTML and CSS, Javascript and PHP
When a CSS or Javascript file is changed the visitor's browser needs to get the latest copy of the file to reflect the changes, but it will have been cached and may cause rendering or functionality issues depending on the changes. To force reloading of the CSS and Javascript file the files should be renamed each time they are changed; this can be automated by using PHP to add the timestamp of the file into the filename.
Parsing Google Analytics data with PHP - TSV files
Posted March 19th, 2009 in PHP
This post shows how to extract data from a TSV (tab separated values) email attachment from data sent by Google Analytics with PHP. This post is virtually identical to the previous post in this series which looked at how to do the same for CSV files, except in this case we use a \t delimiter for the str_getcsv function.
Parsing Google Analytics data with PHP - CSV files
Posted March 16th, 2009 in PHP
This post shows how to extract data from an email attachment from data sent by Google Analytics with PHP. I've covered how to download and extract attachments with PHP's IMAP functions in previous posts and am now coming to the end of this series finally showing how to get the data out of the attachments.
PHP's str_getcsv function
Posted March 16th, 2009 in PHP
While writing my series on extracting data from Google Analytics emails with PHP I discovered PHP has a new function called str_getcsv which works the same way as fgetcsv but extracts CSV data from a string instead of from a file.
Install PHP IMAP on Debian
Posted March 16th, 2009 in Linux/Unix/BSD and PHP
If you are getting the "Fatal error: Call to undefined function imap_open()" error in PHP when using the IMAP functions then they are not installed with your installation of PHP. This post looks at how to install the PHP IMAP functions on Debian and should also work for Ubuntu/Kubuntu/etc and other Debian derived Linux distros.
Downloading Google Analytics data attachments with PHP IMAP
Posted March 15th, 2009 in PHP
This post is part of a series about having Google Analytics data sent by email and then downloading and parsing the data with PHP. Having already set up the automatic sending of data by email this post puts together all the work of the previous posts and actually downloads the attachment by looping through the email messages in the inbox and looking for the specific message to download. The next three posts in the series will then look at how to extract the data from a CSV, TSV or XML formatted attachment.
Function to extract email attachments using PHP IMAP
Posted March 15th, 2009 in PHP
This is a supplementary post in my series about having Google Analytics data sent by email and then downloading and parsing the data with PHP. This post simply contains a PHP function for extracting all the attachments of an email using the PHP IMAP functions into an array.
Parsing Google Analytics data with PHP: A Series
Posted March 13th, 2009 in PHP (Updated May 30th, 2009)
At the start of 2009 I added a "Recent Posts" section to the sidebar navigation of this blog and decided I'd update it using data from Google Analytics. Unfortunately Analytics doesn't have an API to get data but does have a function for sending data by email on regular basis either as a TSV, CSV, XML or PDF attachment.
Therefore a somewhat spordaic series of posts began appearing on this blog showing how to get Google Analytics data by email and use PHP's IMAP functions to read through a mailbox and extract data from the attachments.
PHP IMAP: Looping through messages to find a specific subject
Posted March 5th, 2009 in PHP (Updated March 23rd, 2009)
This post is part of a series about downloading email using the PHP IMAP functions. The ultimate goal of the series is to show how to export Google Analytics data by email, and then use PHP to connect to the mail server, find and download the appropriate message and then load the data into a database. This post looks at how to look for a specific email message using the PHP IMAP functions.
Defining a custom error handler for the PHP ADODB Lite database libary
Posted March 2nd, 2009 in PHP
ADODB Lite is a database abstraction library for PHP. Custom error handlers can be set up for ADODB Lite which will be triggered whenever an error occurs in SQL queries or when attempting to connect to the database. This post looks at how to set up a customer error handler for the ADODB Lite database library.
Open a mailbox other than the INBOX with PHP IMAP
Posted February 26th, 2009 in PHP
This post is part of an on-going series on this blog about connecting to IMAP mailboxes with PHP, ultimately showing how to download and parse reports from Google Analytics. When logging into an IMAP server with PHP the default mailbox is used - usually the INBOX. This post looks at how to list the mailboxes for the IMAP account and then how to connect to one of the other mailboxes.
Using PHP IMAP functions to download email from Gmail
Posted February 23rd, 2009 in PHP (Updated March 13th, 2009)
A couple of weeks back I posted how to use the PHP IMAP functions to download email from an IMAP server. This post looks at how to do the same but downloading IMAP mail from Gmail, which you need to connect to on a different port and use SSL. Earlier today I posted how to enable IMAP mail access in Gmail so you'll need to ensure that's done first.
Command line PHP script to make a backup copy of a MySQL table
Posted February 19th, 2009 in PHP
On Sunday I looked at how to create a copy of a MySQL table with a PHP script and this post looks at how to do the same, but running the PHP script from the command line and passing in the table to copy from and to on the command line.
How to tell if a PHP constant has been defined already
Posted February 16th, 2009 in PHP
You can define constants in PHP with the define() function and can check if a constant has already been defined with the defined() function. This post looks at both functions with some examples.
PHP script to make a backup copy of a MySQL table
Posted February 15th, 2009 in MySql and PHP
Yesterday I looked at how to copy a table with MySQL using some SQL queries. This post looks at how to automate the process with a PHP script saving you having to type in and adjust queries each time you want to back up a table.
Extracting attachments from an email message using PHP IMAP functions
Posted February 9th, 2009 in PHP
This post is part of an ongoing series which aims to show how to extract data from Google Analytics using its scheduled email reports system. I have already looked at how to send Google Analytics data by email, and how to use the PHP IMAP functions to download email. I will also look at using other PHP libraries to download email and attachments, but for now this post looks at how to extract email attachmentsusing the PHP IMAP functions.
Add additional HTML code after the first </p> tag in PHP
Posted February 8th, 2009 in PHP
I recently made some minor layout changes to my blog including the positioning of the advertising from the top of each article to after the first paragraph. In this post I share the PHP code used to add some additional HTML code after the first closing </p> tag in a page's content.
Using PHP IMAP functions to download email
Posted February 5th, 2009 in PHP (Updated March 12th, 2009)
This post looks at how to connect to a POP or IMAP mailbox using PHP's IMAP mail functions and retrive the number of messages in the mailbox, the message headers and body, and then to delete the message.
Install PHP IMAP on CentOS
Posted February 5th, 2009 in Linux/Unix/BSD and PHP
If you are getting the "Fatal error: Call to undefined function imap_open()" error in PHP when using the IMAP functions then they are not installed with your installation of PHP. This post looks at how to install the PHP IMAP functions on CentOS and should also work for Red Hat Enterprise Linux (RHEL), Fedora and other derived Linux distros.
Running PHP scripts as shell scripts
Posted February 2nd, 2009 in PHP
As well as running PHP scripts via a web server such as Apache, you can also run PHP scripts from the command line (it's also possible to write GUI apps with PHP using PHP-GTK, but I'll maybe look at that another time). This post looks at the two ways you can run a PHP script from a *nix based computer (Linux, Unix, BSD, OSX) and the single way you can do it from Windows.
Get the first sentence with PHP
Posted January 29th, 2009 in PHP
Sometimes you may need to get the first sentence from a block of content for use as e.g. the meta description for a page. This post looks at how to get the first sentence using PHP.
How to tell if it's an HTTPS request in PHP
Posted January 26th, 2009 in PHP
This post looks at how to tell whether or not the request being made to the web server is an HTTPS request with PHP. This works for Apache - I'm not sure if the method used here works for other web server types.
Using the is_callable function in PHP
Posted January 22nd, 2009 in PHP
The PHP function is_callable() tests to see if a function or class method can be called, and solves an issue with the method_exists() function which will return true for all methods regardless of whether or not they can actually be called.
Review: Mastering phpMyAdmin 2.11 for Effective MySQL Management
Posted January 21st, 2009 in MySql and PHP
The post has a look at the book "Mastering phpMyAdmin 2.11 for Effective MySQL Management" by Marc Delisle (who is one of the phpMyAdmin developers) and available to buy from the Packt Publishing website, where you can buy it as a hard copy book or an eBook. I am not affiliated with Marc Delisle or Packt Publishing in any way, but was supplied a free copy of the eBook to review.
Using the Zend_Registry in PHP
Posted January 19th, 2009 in PHP
The Zend Framework has a component called the Zend_Registry which allows you to store data into a sort of global space which lasts the duration of the script. Once saved to the registry the data is available throughout the application. This post shows how to save some data to the registry and then retrieve it.
How to check if a class method exists in PHP
Posted January 16th, 2009 in PHP
Last week I looked at how to see if a function exists in PHP and this time will look at how to check if a class method exists in PHP.
How to tell if it's an AJAX request in PHP
Posted January 12th, 2009 in PHP
Some of the upcoming jQuery posts I will be publishing will be making AJAX requests so I thought it fitting to post how to tell if the incoming request is an AJAX request in PHP for the PHP programmers out there. This is useful if your script handles both AJAX and non-AJAX requests and you need to know which sort it is.
How to check if a function exists in PHP
Posted January 9th, 2009 in PHP
You can test if a function exists in PHP with the function_exists() function, which returns true if the function exists and false if it does not. You can then take appropriate action to include the necessary library files or create the function etc.
FCKeditor after capital letters and colons
Posted January 7th, 2009 in Javascript and PHP
I use FCKEditor on my content driven websites to write the content. In version 2.6.3 I have found that the editor inserts characters after capital letters and the colon character, and I don't want this behaviour. This post shows a server-sided solution to the problem using PHP.
Changing the timezone with PHP
Posted December 31st, 2008 in PHP
Since PHP 5.1.0 it has been possible to change the default timezone that PHP uses using the date.timezone configuration, which can be set for the entire server in php.ini or httpd.conf, on a virtualhost by virtualhost basis, or using the ini_set() function. This post looks at how to change the timezone in PHP using the ini_set function.
PHP list of php.ini directives
Posted December 30th, 2008 in PHP
PHP allows you to set a variety of configuration settings in either the web server's configuration (e.g. Apache's httpd.conf or .htaccess) and using the ini_set function. The PHP manual contains a complete list of php.ini directives and this short post is simply my place of bookmarking the list, as I often need to refer to it.
RSS with PHP - Don't forget to set the Content-Type
Posted December 27th, 2008 in PHP
Just a quick little post to remind that PHP/Apache sets the content-type header by default to text/html. If you are serving other content, such as an RSS feed, you need to remember to change the content type to the appropriate format.
Google Analytics Screen Widths with PHP (Internet Explorer)
Posted December 24th, 2008 in PHP
A week ago I looked at copying and pasting the screen resolution data from Google Analytics and parsing it to get just the screen widths and percentages with a PHP script using Firefox. The copy-paste format is different for Internet Explorer so this followup postmodifies the script for IE.
Google Analytics Screen Widths with PHP (Firefox)
Posted December 17th, 2008 in PHP
Google Analytics has screen resolution information but groups them by both width and height. I was wanting to know the percentage of visitors by width so created a PHP script to parse text copied from the Analytics page and sum up the percentage by screen width.
Set the content encoding type with PHP headers
Posted December 13th, 2008 in PHP
Although you can set content type in the HTML <head> section of a web page the web server can also send the content type as one of its headers, and it can cause unexpected consequences when rendering a page in a browser. This post looks at how to set the content encoding type header with PHP.
Check if a class exists with PHP
Posted December 11th, 2008 in PHP
PHP has the class_exists() function to check if a class has already been defined. You may need to take action depending if a class exists or not, and need to watch out for "gotchas" when also using __autoload().
Method chaining with PHP
Posted December 7th, 2008 in PHP (Updated January 5th, 2009)
Having used the Zend Framework on a couple of projects and spent a lot of time reading the documentation I discovered the use of method chaining. This post looks at how to use method chaining in PHP.
Validating numbers with PHP
Posted November 25th, 2008 in PHP
Last week David Walsh posted about validating numeric values and digits with PHP on his excellent blog. I posted my own comments about his particular post but they never appeared so I decided to post a better response here.
5 PHP job and programming experience tips
Posted November 20th, 2008 in PHP
I've had a couple of people email me recently asking for tips about looking for PHP work in New Zealand and getting programming experience etc. This post looks at 5 ideas I had for them. Note some are specific to New Zealand but use local job websites and mailing listsinstead of the ones suggested here if you're from another country.
Get the class name and parent class name in PHP
Posted November 19th, 2008 in PHP
Sometimes you need to be able to get the class name or parent class name of an object in PHP to do something conditionally etc. This post looks at how to do this.
Zend Framework 1.7.0 released
Posted November 18th, 2008 in PHP
The Zend Framework 1.7.0 has been released and is available for immediate download. This release fixes 288 bugs introduces many new components and features. It can be downloaded from the Zend Framework Downloads Page and read more about this release on Matthew Weier O'Phinney's blog.
How to use PHP's __autoload function
Posted November 12th, 2008 in PHP (Updated June 15th, 2009)
PHP introduced the __autoload() function in version 5 which is called whenever the code tries to use a class that has not yet been defined. You simply put some code into __autoload() to include the appropriate class file and don't have to bother about manually including those files. This post looks at how to do this with examples.
CodeIgniter loading models error when changing web host
Posted November 9th, 2008 in PHP
I've been in the process of moving a website that uses the CodeIgniter PHP framework from one of my servers to another provider's server and kept getting"Fatal error: Call to a member function on a non-object" type errors when trying to use some of my models. This post looks at what was causing the issue.
Using hash algorithms in PHP
Posted November 7th, 2008 in PHP
PHP has a number of hashing algorithms available for creating hash values of files and strings. A hash is a non-reversible fixed length string which has a number of applications, such as storing a password in a database in a way that can not be read. This post looks at how to show the available hashes in PHP and how to use the hash() function to generate a hash value.
PHP fatal libpng error: zlib error
Posted November 4th, 2008 in PHP
While working with some image functions in PHP I changed the output image from JPEG to PNG (from the ImageJPEG to ImagePNG functions) and got the error message "gd-png: fatal libpng error: zlib error". This post looks at the cause of the error and the solution.
404 Page not found with PHP
Posted October 29th, 2008 in PHP
A PHP script will normally return a 200 HTTP code to the browser which means the page has been successfully retrieved but it's possible to also return other status codes when redirecting (e.g. doing a permanent redirect) or a 404 to indicate a page has not been found. This post looks at how to do a 404 page not found error with PHP.
Check if a PHP script is already running
Posted October 28th, 2008 in PHP (Updated October 29th, 2008)
If you have long running batch processes with PHP that are run by cron and you want to ensure there's only ever one running copy of the script, you can use the functions getmypid() and posix_kill() to check to see if you already have a copy of the process running. This post has a PHP class for checking if the script is already running.
PHP SoapClient "Unable to parse URL" error
Posted October 23rd, 2008 in PHP
While testing a web service I was creating in PHP using the PHP SoapClient and SoapServer classes I got the error "Unable to parse URL" from a WSDL file generated using Zend Studio. This post looks at the error and how to fix it.
phpMyAdmin "Cannot start session without errors"
Posted October 22nd, 2008 in PHP
Yesterday one of my customers reported their phpMyAdmin install was showing the error message "Cannot start session without errors" and they were unable to even get to the phpMyAdmin login page. This post looks at what caused the issue and how to fix it.
Hide selected databases in phpMyAdmin
Posted October 21st, 2008 in MySql and PHP
A few days ago I posted how to hide the information schema in phpMyAdmin and of course using the advice in that post you can hide any database you want. The hide_db phpMyAdmin configuration option is a regular expression so you can use it to hide several databases, and this post looks at how to do this.
Hide the information schema in phpMyAdmin
Posted October 11th, 2008 in MySql and PHP
When you log into phpMyAdmin you can only see the databases for which you have access rights, as well as the information_schema database. It is possible to hide this database if you want to. This post looks at how to hide the information_schema database in phpMyAdmin.
Intialise an array in PHP with default values
Posted October 7th, 2008 in PHP
PHP has a useful function for initialising an array with default values called array_fill(). This post looks at how to use the array_fill function and the output of these arrays with the print_r function.
Change the user agent string in PHP
Posted October 2nd, 2008 in PHP
Most of PHP's file functions can be used to open local files as well as remote files via HTTP etc. By default the user agent string passed when making an HTTP requestis an empty string but it is possible in PHP to change the user agent string to something else. This post looks at how to do this.
Return information from PHP print_r instead of displaying it
Posted September 30th, 2008 in PHP
print_r is a useful PHP function for displaying the values of an array or object when debugging etc. Having been a PHP programmer since the days of PHP 3.x I didn't realise this function had added the capability to return the information as well as print it from PHP 4.3.0 and only discovered this in the last few days. This post gives a brief overview of PHP's print_r function.
New Zealand daylight savings starts on Sunday
Posted September 26th, 2008 in Linux/Unix/BSD and PHP
The dates that New Zealand daylight savings starts and ends changed in New Zealand last year and it caused a lot of issues with many servers and desktop computers which hadn't had updates made available in time for the change. Daylight savings starts this Sunday in New Zealand on Sunday September 28th and I thought it a good idea to check the servers I manage are all correct and ready to handle the changeover.
Work out if a number in PHP is odd or even
Posted September 23rd, 2008 in PHP
Sometimes you might need to know if a number is odd or even, and this post looks at how to determine if a number is odd or even in PHP using the modulus arithmetic operator.
Fix the mbstring PHP extension not found error in phpMyAdmin
Posted September 18th, 2008 in PHP
phpMyAdmin is a web based front end to MySQL written in PHP. It likes to have the mbstring extension loaded if using a multi byte character set and will display warnings when you first log into phpMyAdmin if it is not available. This post looks at the error message and how to install the mbstring extension for PHP on CentOS / Red Hat Enterprise Linux using yum.
Update PHP from the command line with YUM
Posted September 16th, 2008 in PHP
It is important to keep software up to date on your computer and web servers. I manage a few webservers which run Apache/PHP/MySQL on CentOS, a Red Hat Enterprise Linux derivitive, and use YUM to install software and keep it up to date. This post looks how you would just do an update to PHP using YUM on CentOS.
Optimize tables in MySQL automatically with PHP
Posted September 7th, 2008 in MySql and PHP
In previous posts I looked at how to optimize a MySQL table from the MySQL command line interface and from phpMyAdmin by using the optimize [tablename] command to free up unused space. In this post I will look at how to do this with a PHP script which could be run periodically to optimise all non-optimal MySQL tables.
PHP CLI counter for long running processes
Posted August 29th, 2008 in PHP
I've been doing a reasonable amount of PHP CLI (command line interface) programming recently for getting stuff from log files and converting large numbers of images. Some of these processes can some quite a while to run, sometimes as long as several hours, so it can be useful to know roughly how far through the process it is.This post looks at how to output a counter to the command line from PHP which replaces itself on each iteration.
Sending email with Zend_Mail
Posted August 23rd, 2008 in PHP
The Zend Framework is a PHP framework with many individual components that do not require you to use the whole framework. In this post I will look at how to send an emailusing the Zend Framework's Zend_Mail component.
Using the FCKEditor HTML Editor with PHP
Posted August 20th, 2008 in Javascript and PHP
FCKeditor is an in-browser WYSIWYG HTML editor and I use it on a number of websites including in the blog admin for this website. This post looks at how to put FCKeditor into a web page using PHP. In future posts I will look at some of the other functions and features of FCKeditor.
PHP Date Constants
Posted August 16th, 2008 in PHP
When writing my "Get a list of all available constants with PHP" post I discovered there are a number of usful date format constants in PHP which can be used with the date() function. (Refer to my "Formatting Dates with PHP" post for more details about the PHP date function). This post looks at these constants and how to use them with the date function. Please note that these constants have only been present in PHP since 5.1.1.
Type casting with PHP
Posted August 10th, 2008 in PHP
PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Variables coming from get/post and cookies etc are generally cast as strings rather than other types and there are often other times when you need to specifically cast a type in PHP as e.g. an integer. This post looks at how to type cast in PHP and some of the results than can come about from type casting.
303 redirect with PHP
Posted July 31st, 2008 in PHP
When you submit a form using a web browser the script processing the form often redirects after it has done its work to a thank you page (or similar). The response type people usually use when coding with PHP is a 302 redirect because it's the default used when issuing a Location: header. The problem with this is on a page refresh the browser is likely to submit the form again. Instead you can use a 303 redirect to prevent the form being submitted again. This post looks at what a 303 response code / redirect is and how to send one with PHP.
Image headers with PHP
Posted July 22nd, 2008 in PHP
If you ever need to send an image file with PHP from the web server to the web browser you need to add an additional header using the header() function so the browser knows it's an image and not regular HTML. This post looks at the headers you need to use.
Get a list of all available constants with PHP
Posted July 17th, 2008 in PHP
PHP has the function get_defined_constants() which allows you to get a list of all the available constants in an array. This post looks at how to use the get_defined_constants() function and example output from it.
Get a list of all available classes with PHP
Posted July 10th, 2008 in PHP
PHP has the function get_declared_classes() which allows you to get a list of all the available system and user defined classes in an array. This post looks at how to use the get_declared_classes() function, the output from it and how to sort the list into alphabetical order.
Get an image size with PHP
Posted July 9th, 2008 in PHP
PHP has a function called getimagesize() for getting the width and height from an image. I'm always forgetting the name of the getimagesize() function so I decided to write a post about it and showing some examples and that way maybe I'll remember in the future...
Get a list of all available functions with PHP
Posted July 3rd, 2008 in PHP
PHP has the function get_defined_functions() which allows you to get a list of all the available system and user defined functions in an array. This post looks at how to use the get_defined_functions() function,the output from it and how to sort the list into alphabetical order.
List compiled modules with the PHP CLI
Posted June 29th, 2008 in PHP
The PHP CLI (Command Line Interface) has a number of flags which can be passed to it for executing PHP scripts, getting information about your PHP install and so on. This post looks at how to get a list of the modules compiled into the PHP CLI. Note that depending on the way your distribution/package has compiled PHP, the list of modules available for the webserver and the CLI may be different.
JpGraph Error Can't access PHP_SELF, PHP global variable
Posted June 27th, 2008 in PHP
One of the sites I work on uses JpGraph to generate graphs for their reports. A few months ago we moved the site from an old server with PHP 4 to a new server with PHP 5 and started getting the error "JpGraph Error: Can't access PHP_SELF, PHP global variable". This post looks at the error and a couple of possible solutions to it.
Command line arguments for the PHP CLI
Posted June 19th, 2008 in PHP
The PHP command line interface or CLI allows you to run PHP scripts from the command line. It also has a number of flags that can be passed to it which allow you to see which modules are available, do a syntax check on a file, view PHP information etc. This post gives a basic list of the command line arguments available for the PHP CLI and in later posts I will look at some of the options in more detail.
Command line arguments with a PHP CLI script
Posted June 15th, 2008 in PHP
PHP has the getopt() function for getting options or command line arguments from a CLI PHP script. This provides a simple way to get values from the command line like e.g. "-a foo". This post looks at how to do this. More details can be found in the PHP getopt() manual page.
PHP function to check if a MySQL table exists
Posted June 14th, 2008 in MySql and PHP
Yesterday I posted how to check if a MySQL table exists using show tables or the MySQL information schema. Today I am posting a simple PHP function I created which you can use to test if a table exists.
PHP is not showing any error messages
Posted June 10th, 2008 in PHP
Help! There are errors in my PHP code and there are no error messages being displayed. I've even set error_reporting to E_ALL and still PHP is not displaying the errors. How do I debug it? How do I show the errors? This post looks at the very simple answer to these questions.
phpMyAdmin "operation could take a long time" message
Posted June 4th, 2008 in PHP
In phpMyAdmin in some of my databases when I am browsing a table and click the button to take me to last page of records I get a Javascript message telling me "This operation could take a long time. Proceed anyway?" with "OK" and "Cancel" buttons. This post looks at why the message pops up and how you can control when it appears.
Set the default font size for phpMyAdmin
Posted May 17th, 2008 in PHP
phpMyAdmin is a web based administrative tool for the MySQL database server. After logging in to phpMyAdmin there's a drop down box to select the font size as a percentage which always defaults to 100% and is too big for my liking. After changing the value, the font size is remembered as a cookie so the next time you log in the font size chosen is remembered. However the font size cookie drops off eventually and when logging into a phpMyAdmin that I haven't logged into for a while I find it annoying having to change the font size each time. This post looks at how to change the default phpMyAdmin font size.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
Install APC for PHP on Linux
Posted February 6th, 2008 in PHP (Updated November 5th, 2008)
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.
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.
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 92 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 92 files, one for each day of each month in under 4 minutes.
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.
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.
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.
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.
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 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.
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.
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.
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).
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.
Javascript and CSS file timestamps with PHP
Posted November 1st, 2007 in HTML and CSS, Javascript and 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.
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.
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 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 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.
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.
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.
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.
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.
