From the category archives:

PHP

My LOLCode Interpreter

October 31, 2007

LOLCode Book

I had 2 hours to kill so I thought I’d write a LOLCode interpreter.

I know there’s already a few LOLCode interpreters around, but I couldn’t help myself. Plus I’ve always been interested in interpreter design.

It’s still a work in progress at this stage, so a lot of the language hasn’t been fully implemented.

Here are two working programs…

Hello World


BTW the classic hello world program
HAI
VISIBLE "Hello world!"
KTHXBAI

Output:

LOLCode Hello World

Accepting User Input


HAI
VISIBLE "Hello there, what is your name?!"
I HAS A NAME ""

BTW ask for the users name
GIMMEH NAME

BTW welcome the user
VISIBLE "Welcome to LOLCode " N NAME N "!"
KTHXBAI

Output:

LOLCode Accepting User Input

{ 0 comments }

PHP Command Line Tip

September 11, 2007

Here’s a quick tip for working with the command line in PHP.

If you’ve ever run a PHP script via the command line, you would have noticed that output from the script is not printed until the script has finished.

If you need your output displayed in real time, you can open a stream to the command line…

$stdout = fopen('php://stdout', 'w');

Simply write any output to the stream and it will be printed on the command line in real time…

fwrite($stdout, "Hello CLIn");

{ 0 comments }

Facebook Code Analysis

August 16, 2007

Facebook Code Analysis

Most developers have now heard about Facebook’s leaked index.php source code, which was anonymously posted here. If you haven’t seen it already, there are a number of links listed on Techcrunch.

I’ve seen a few bloggers criticize Facebook developers for using procedural programming rather than classes and object oriented techniques. I’m not exactly sure why they’ve chosen to develop the site like this; but I am going to take a guess and say it was to improve speed and efficiency.

Object oriented programming was first introduced to PHP in version 4. However, the language wasn’t originally designed around objects and classes, so the implementation was clunky and awkward. This meant that procedural code was often much faster than object oriented code.

Considering the size and popularity of social networks, I’m not surprised they chose procedural code over objects. That tiny boost in performance would easily outweigh the advantages of using classes.

Fortunately, most (if not all) issues with objects have been solved in PHP 5, which is now closer to a truly object oriented language.

{ 0 comments }

Here’s an easy way of filtering empty variables from an array, without using a loop or multiple if statements.


<?php

$var1 = ;
$var2 = ‘dog’;
$var3 = ;
$var4 = ‘fish’;

$my_array = array($var1, $var2, $var3, $var4);

print_r(array_filter($my_array));

?>

Output:
Array ( [1] => dog [3] => fish )

{ 0 comments }

Handling Errors in PHP

June 27, 2007

PHP errors are ugly and should never be shown to your users. Luckily with PHP we can handle errors our own way using the set_error_handler function.
I’ll start by defining the error handling function which will write the error to a log file and stop execution.

function handleError($level, $error, $file, $line)
{
  if ($logfile = fopen(‘log.txt’, ‘a’))
  {
    fwrite($logfile, date(“F j, [...]

Read the full article →

PHP Function Argument Lists

June 21, 2007

A lot of PHP developer’s don’t know about PHP’s support for variable-length argument lists.
This is a quite an easy feature to use and can be useful when you’re not sure how many arguments a function should accept.
To declare a function with a variable-length argument list, declare it with an empty argument list.
function add_scores()
Calling the function [...]

Read the full article →

Use AJAX Sparingly

June 20, 2007

AJAX is a only great technique when used in moderation. It belongs in forms and page widgets, but should never be used for navigational purposes.
Call me a traditionalist, but webpages are pages, not desktop applications. Changing the way a UI component works on your website is unnecessary and confusing for your users.
Normal links shouldn’t [...]

Read the full article →

Regular Expressions in Ruby and PHP

June 14, 2007

Here is a short comparison between Ruby and PHP. The task was to print the location of all HTML links in a webpage using regular expressions.
If anyone knows of a cleaner or more efficient way to do this in Ruby or PHP, please post it in the comments!

Ruby

require ‘net/http’
#connect and get the webpage
host = [...]

Read the full article →

MD5 Encryption

May 29, 2007

MD5 is a popular hash function which is often used to encrypt passwords in web applications. In most applications, when a user enters their password, it is encrypted and compared to the one stored in the database. If both md5 hashes match, the user is granted access.
This approach is generally considered to be quite secure [...]

Read the full article →

Data Visualization

May 24, 2007

Data visualisation isn’t a new concept. For decades, scientists and geographers have been been relying on data visualisations to present their huge amounts of data.
Only recently has it started gaining interest on the web, and I believe we have Digg Labs to thank for that.
Since releasing their API, a number of great visualisations have surfaced. [...]

Read the full article →