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

Printing relative dates in php

A couple of days back while writing some date code for a messaging service, I required to print the date of the messages in a relative format – ‘today, ‘yesterday’, 3 weeks ago’ etc. I wrote a small function for the same. A sample run of the function is shown below.

 
echo DateToWords(time()) . "<br />";
echo DateToWords(time() - (3600 * 24 * 1)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 4)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 7)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 14)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 100)) . "<br />";
echo DateToWords(time() - (3600 * 24 * 366));

And the output for the above. For dates above 1 year it returns the actual date.

 
today
yesterday
4 days ago
1 week ago
2 weeks ago
14 weeks ago
06-17-2009

(more…)

Read More

Anonymous functions in PHP

Anonymous functions are common in various modern languages, Ruby and Javascript being the popular one. But until version 5.3 PHP lacked true anonymous functions. Although newbie programmers are hard-pressed to find a suitable application for anonymous functions, they are indispensable if you do a lot of OOP, and can provide some elegant solutions to some particular problems.
(more…)

Read More

How to Fix PHP Vulnerabilities (So Your Site Won’t Get Hacked)

As a programming language, PHP has many advantages but security has always been a major issue. Partially these security problems are inherent to the language itself because PHP was meant to be an easy and powerful programming language, while security came second. However, when you add bad coding and non-adherence to even the basic security rules, the situation gets out of control.

Fortunately, it is possible to fix PHP vulnerabilities and make PHP applications more secure. Some of the defenses are common for all programming languages, while others are found only in PHP. Here are some of the best defenses you have when you want to fix PHP vulnerabilities and make your site more secure.
(more…)

Read More

Generating random data on the client side

Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes.

Faker.js is a JavaScript implementation inspired by Benjamin Curtis’s Ruby Gem Faker and Perl’s Data::Faker that lets you generate commonly required data quickly. You can check the demo page to get an idea.
(more…)

Read More

For whom do we develop software!

badaman_hands_on_clayInterface design is hard. Which is why most programmers turn a blind eye to it. During collaborative development I frequently encounter fellow programmers remark something to the following effect: ‘…do not worry, the users are not idiots, they will understand for what these buttons have been provided, no need to provide tool-tips or any help, lets get these code working and show it to the client.’
Interface design or rather usability design is usually left as an after thought; a colorful facade that you stick on to your backend code.

The following two videos provide a stark reminder, that for most of the time these are the people we develop software for.
(more…)

Read More

Geographical information lookup using GeoNames

Geographical information integration is rapidly becoming an integral part of many websites. People use geographic data for a wide variety of applications. From location based content targeting, censoring information by geographic areas to analyzing website traffic by region. It is surprising how much free geographic information is available on the web. GeoNames is one such service.
(more…)

Read More

Deleting pesky blank lines from source files

Blank lines are an eye sore for source files. During the last few weeks I’ve been shuttling php and html files from various host servers and my PC. During the whole ordeal the source files kept adding a huge number of blank lines. At one point an ‘if’ construct in one of my PHP code was about 60 lines away from the next starting brace. This was all caused by the non standard newline character, also known as a line break or end-of-line (EOL) character. As we all know, it is a special character or sequence of characters signifying the end of a line of text. Unfortunately the actual codes representing the newline differ across operating systems, which creates problems such as the above when exchanging data between systems. If you think you don’t use *nix systems and are therefore free from these problems then you are wrong. Even though you may be using Windows, most host servers are Linux based, and so transferring files back and forth from these servers can create these kind of problems.
(more…)

Read More

Google Translation PHP wrapper

UPDATE
Google has deprecated its Translation service, hence it will not be available for download anymore.

Google translation is an interesting service. Not only can you do language translation, you can also detect the language of a particular text. I recently needed to create a WordPress plugin to translate post titles from one language to another. As the translation API is only available for Java and Javascript, I decided to create a quick one for PHP. In this post we will see how to translate text from one language to another in a simple and quick way using the created class.
(more…)

Read More

Benchmarking WordPress SQL using FirePHP

firephp_thumbBuilding and running a WordPress site is a simple matter. WordPress is a considerably fast CMS system, until you start to add more and more plugins and one day you notice that Worpdress has started to slow down. It may be the case that SQL queries within some plugins are not optimized and are taking an increased amount of time executing them, this can considerably slow down your site. The first thing you can do to rectify the situation is to find out where exactly the bottleneck resides by analyzing the time each SQL query takes to executes. Some inquisitive people among you may also be interested in knowing in what sequence the WordPress SQL queries themselves are being run. Not that all bottlenecks occur due to unoptimized SQL, most are due to poor coding practices. Whatever the reason; the following post will show you how to look inside the SQL query execution of WordPress.
(more…)

Read More