UltraMega Tech.
14Jul/095

Generating (X)HTML Documents Using DOMDocument In PHP

PHP 5 includes a powerful set of DOM manipulation classes that gives you full control over XML documents. This functionality behaves very similar to JavaScript's DOM manipulation engine. In this tutorial, we'll explore the DOMDocument class by generating an entire (X)HTML page without writing a single bit of raw markup. This may not be practical for most applications, but it should give you a good idea of how the basic DOMDocument methods work.

Tagged as: , , Continue reading
7Jul/0910

Fetch Twitter Updates in PHP

Would you like to integrate Twitter into your website? In this tutorial, I'll explain how to access the Twitter API to fetch a user's updates using raw PHP (version 5.0 or greater). This will be a very basic use of the Twitter API, but it should give you a starting point if you need to do something more advanced. There are also some Twitter libraries that will let you do the same thing without knowing how it all works.

We will be utilizing the statuses/user_timeline method of the Twitter REST API. Basically, this is a special URL that returns the requested data in the requested format. In this case, we will request the user_timeline as XML, so our URL looks like: "http://twitter.com/statuses/user_timeline.xml?screen_name=username".

23Jun/090

What’s new in PHP 5.3?

With last week's announcement of RC4, the final release of PHP 5.3 is imminent. So what can we expect in this version? Here I'll outline the biggest and most interesting changes and additions. This is definitely not exhaustive since there are tons of minor changes and improvements in this version, but these are some of the ones I found interesting.

Tagged as: , Continue reading
16Jun/094

5 Tips for Writing Cleaner PHP Code

There are many benefits to writing clean code for any project, no matter how small. The most obvious benefit is that it is easier for you to read and edit, especially when you come back after a long break. This is even more important when someone else comes in to modify your code. The importance of clean code becomes most apparent when you need to find an error, especially a syntax error. In the end, cleaner code means less time reading and more time coding!

These are 5 tips that I use and recommend. I know there are plenty of lists like this, but I hope you learn at least one new trick...

3Jun/097

Snippet: Converting Seconds to Readable Time (PHP & JS)

Sometimes, you might need to convert an integer representing seconds into a format that is easier to read. These functions can be used to turn a number of seconds into a simple format of HH:MM:SS, with leading zeros (ex. 15272 = 04:14:32). This can be used for countdown scripts, which is why I also include both a PHP and a JavaScript version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function formatTime($secs) {
   $times = array(3600, 60, 1);
   $time = '';
   $tmp = '';
   for($i = 0; $i < 3; $i++) {
      $tmp = floor($secs / $times[$i]);
      if($tmp < 1) {
         $tmp = '00';
      }
      elseif($tmp < 10) {
         $tmp = '0' . $tmp;
      }
      $time .= $tmp;
      if($i < 2) {
         $time .= ':';
      }
      $secs = $secs % $times[$i];
   }
   return $time;
}
29May/091

Snippet: Autoloading Objects (PHP 5)

When writing the PHP that powers TempServers, I wrote the core functions into classes for better organization and efficiency. To make things further organized, each class is stored in individual source files, no matter how small. The problem here is keeping track of which classes are used where, in order to include the appropriate source files. Fortunately, PHP 5 offers a solution: autoloading.

This comes in the form of the __autoload function which automatically gets called if a class is called that does not exist. Here's what it looks like:

function __autoload($class) {
    require_once(strtolower($class) . '.class.php');
}

Usage

  1. Place each PHP class in individual files named after the class followed by .class.php (Example: class MyClass goes in a file named myclass.class.php).
  2. Include the code snipped above into any file that might use one of your classes.

For more details, check out the PHP manual: http://www.php.net/manual/language.oop5.autoload.php