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
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 change page content without warning and pages shouldn’t extend when you reach the bottom.
Use AJAX sparingly if you want to make a better user experience.
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 = Net::HTTP.new('www.site.com.au', 80)
body = host.get('/index.php', nil ).body
puts "Links found..."
#find link URIs
links = body.scan(/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/)
#print all link URIs
links.each {|id,uri| puts uri}
PHP
<?php
$page = file_get_contents('http://www.site.com.au/index.php');
// find links
preg_match_all('/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/', $page, $links);
// links found
foreach($links[2] as $link)
{
print "$linkn";
}
?>