8 randomly useful php tricks

Mon 22 February 2010

In keeping with my series of 8's.

1 ) debug_print_backtrace()- I use this one a lot, print a debug-style list of what was called to get thethe point where this function is called. Very, very useful.

2 )__autoload()- Called when you attempt to load a class that hasn't been defined. Example: I try load class xyz, the class has never been imported, I use the autoloader to attempt an import of xyz.php. Probably not the best technique for readability or clarity in code, but has it uses.

3 ) ip2long()- Don't store IP addresses as strings on a db use this to store them as a long.

4) __toString() - If an object is ever used as a string, you can define what's returned here. Again you can find it's uses once you realise these things are possible. :)

5)get_browser() - Easily get your hands on the users browser-type. Some leave this process to the browser end but can be useful to get this info server side.

6) More of a total dirty hack(come on, it's PHP). Having problems with that annoying 'headers already sent' error. Don't bother to fix the underlying problem, just omit the closing php tag! It's totally valid(this list is really becoming things I hate about PHP….)

7) natsort() - This will sort lists of things in ways humans will understand. Good idea when displaying drop-down lists etc for users. Again, you might find a use. :)

8) metaphone() - Again one of those random things PHP does, you may never use thisbut this will return how a word sounds. EX: Bomb->BM. Might be useful for comparing words that sounds similar, this one's really obscure and I honestly haven't actually found a use for it yet. :)

Take a look at the PHP magic methods for some other ideas. :)


Category: PHP Tagged: php tricks