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:

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:

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");
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.
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 )
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, Y, g:i a") ." $error on line $line in $filen");
fclose($logfile);
}
die('An error has occured.');
}
Then set it as the error handling function.
set_error_handler('handleError');
Trigger an error to see if it works.
echo 100 / 0;
You could also get your function to notify you via email. Or you could forget about logging, and just display better looking errors…
function handleError($level, $error, $file, $line)
{
$errorhtml = '<div style="border: 4px solid #bfbfbf; font-size: 12px; font-family: arial, sans-serif; padding: 1px">';
$errorhtml .= '<div style="background-color: #a3503f; color: #ffffff; padding: 3px;">ERROR</strong></div>';
$errorhtml .= $error.'<br />';
$errorhtml .= 'Line: '.$line.'<br />';
$errorhtml .= 'File: '.$file;
$errorhtml .= '</div>';
echo $errorhtml;
}

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 with multiple arguments is easy…
add_scores(88, 37, 99, 32);
Processing the arguments inside the function is also an easy task using the func_get_arg(), func_get_args() and func_num_args() functions.
function add_scores()
{
return array_sum(func_get_args());
}
Output: 256