Disabling the silence @-operator in PHP

PHP supports one error control operator: the at sign (@). When prepended to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:

$var = $_GET['data'];

If the ‘data’ parameter is not defined the expression will generate an error.

Notice: Undefined index: data in /var/www/test.php on line 9

You can hide the error using the silence @-operator.

$var = @  $_GET['data'];

Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the Scream Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.
(more…)

Read More

Reading MP3 file tags in PHP

I recently had a small task to scan some MP3 files on a server to check whether the files had any metadata in them, and add them if necessary. All MP3’s have a metadata section that allows information such as the title, artist, album, track number etc. to be stored in the MP3 file itself. The metadata is stored in the ID3 format. Of course I could have used a desktop tag editor, but the files where on the server, which frequently kept changing. So a server solution was the only way to go.
(more…)

Read More

Detecting user agents in php

Every time you use your browser to access a website a User-Agent header is sent to the respective server.
Detecting user agents on the server can be useful for many reasons.

1. Browsers Quirks – Despite the standardization in browsers, there will remain some quirks in various browsers that you will need to iron out on a regular basis.
2. Personalize Content – It may be required to deliver different type of content depending on the browser type (although it is usually not recommended); whether mobile or otherwise.
3. Illegal Access – Prevent bandwidth hogging bots and poorly programmed clients from downloading your content.
(more…)

Read More

Beautifying XML documents

I frequently write php code to access various web services and the most common response data I encounter is in xml, which most of the time is not formatted. I use xmlPad to format and analyze xml documents, but many times I need to format xml documents on the production server wherein xmlPad is of no use. What one needs is a library that would allow you to beautify your untidy xml documents within your php code. XML_Beautifier provides that solution.
(more…)

Read More

Search & replace in files using php

Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. File_SearchReplace is a pear package that helps you search/replace in files through a nice object oriented interface.
(more…)

Read More