From the category archives:

Programming

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, 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;

}

php error

{ 0 comments }

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

{ 0 comments }

Use AJAX Sparingly

June 20, 2007

AJAX

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.

{ 0 comments }

Ruby

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";
}

?>

{ 0 comments }

I’ve just started reading Why’s (Poignant) guide to Ruby.

The writing style is a little different to most technical books but that’s what makes it interesting.

Why's (Poignant) guide to Ruby.

Here’s an example from chapter 1…

One day I was walking down one of those busy roads covered with car dealerships (this was shortly after my wedding was called off) and I found an orphaned dog on the road. A wooly, black dog with greenish red eyes. I was kind of feeling like an orphan myself, so I took a couple balloons that were tied to a pole at the dealership and I relocated them to the dog’s collar. Then, I decided he would be my dog. I named him Bigelow. We set off to get some Milkbones for Bigelow and, afterwards, head over to my place, where we could sit in recliners and listen to Gorky’s Zygotic Mynci. Oh, and we’d also need to stop by a thrift store and get Bigelow his own recliner.

I thought the author (Why) did a great job of explaining global variables to beginners…

Some parts of your program are like little houses. You walk in and they have their own variables. In one house, you may have a dad that represents Archie, a travelling salesman and skeleton collector. In another house, dad could represent Peter, a lion tamer with a great love for flannel. Each house has its own meaning for dad.
With global variables, you can be guaranteed that the variable is the same in every little house.

{ 0 comments }