Archive for the 'PHP' Category

Pajama: Client and Server Validation Simplified

When implementing an HTML form that requires validation, a developer must make a decision: should I use client-side validation, server-side validation, or both?

In this article I will give a quick overview of the pros and cons of these validation techniques, as well as introduce my solution to the problem: Pajama.

Read more »

Counting Syllables and Detecting Rhyme in PHP

I was looking at a Software Developer posting on the FreshBooks careers page the other day, and near the end of the “How to apply” instructions there was a curious sentence:

“If you want to prove you’re really paying attention, include a verse of ottava rima and a link to your GitHub profile with your application and you’re guaranteed to have your application reviewed by our Software Development Manager.”

I’d never heard of the ottava rima rhyming stanza form before, but a quick trip to Wikipedia remedied that. Essentially, an ottava rima stanza must satisfy three rules:

  1. Each stanza must have 8 lines,
  2. The lines must be iambic pentameter,
  3. The stanza must have the rhyming format a-b-a-b-a-b-c-c.

Here is an example of an ottava rima stanza by Frere (as given in the Wikipedia article):

But chiefly, when the shadowy moon had shed
O’er woods and waters her mysterious hue,
Their passive hearts and vacant fancies fed
With thoughts and aspirations strange and new,
Till their brute souls with inward working bred
Dark hints that in the depths of instinct grew
Subjection not from Locke’s associations,
Nor David Hartley’s doctrine of vibrations.

The simplicity of the rules got me thinking: how hard would it be to write a program to check if a poem stanza is ottava rima?

In this article, we will write a simple ottava rima detector in PHP.

(If you’re not interested in the details, you can skip to the code at GitHub.)

Read more »

How to do an AJAX search with jQTouch, Part 2

In the last installment of How to do an AJAX search with jQTouch we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls.

This article assumes you have read the first part of this series.

Read more »

How to query posts by template in WordPress

Ever wanted to filter a query_posts call by template? If you’re reading this post, you probably have or need to right now. You probably went to the query_posts documentation and scanned for something like “template=foo” and were deeply disappointed. Then maybe, in an act of desperation, you started to trudge around the plugin library. Stop! Stop right there! You don’t need a plugin to do this. Everything you need is already in WordPress.

Read more »

PHP interfaces and optional parameters

Interfaces in object-oriented languages like Java, C# and PHP are useful language constructs for ensuring that objects will respond to certain methods. However, unlike Java and C# (until recently), PHP has optional parameters. As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement interfaces in PHP.

Read more »

Adding Extension Methods to PHP

If you’ve ever used the .NET Framework version 3.5 of later, you’ll probably have encountered extension methods. Microsoft describes extension methods as:

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

In this article, I will show you how to write a base class that will allow you to add methods to any PHP class that inherits from it at runtime. You will be able to call these methods transparently, without any special syntax.

Read more »

How to do an AJAX search with jQTouch, Part 1

jQTouch is a pretty nifty little jQuery plugin for making websites look like native iPhone apps. Unfortunately, beyond a bunch of examples included with the distribution, there’s not a whole lot of online documentation written for jQTouch. Having started to use jQTouch for a professional project, I thought I’d help remedy this deficiency by posting what I learned along the way.

We’ll start with something fairly straightforward by making a jQTouch app that makes an AJAX call to a PHP script to search a database. It then displays the results of that search in the jQTouch app. This article is split up into two parts. The first part deals with the jQTouch UI and the second part deals with the PHP script and the supporting JavaScript.

This is part 1 of a two part series.

Read more »

Convert PHP date format string to .NET DateTime format string

I recently had to convert some JavaScript code I’d written that used a PHP-style date format string to a .NET DateTime-style date format string.

It would’ve been a helluva lot faster to have had some sort of conversation table. After a little searching on Google, I found nothing so I decided to make one myself. Behold, the fruits of my labour!

Read more »

Consuming a J2EE Web Service with PHP SOAP

I recently had to do some work for a company which involved using PHP to consume a J2EE-based WSDL web service. The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected.

Read more »