Introduction
I have been writing PHP code for quite some time now. Though I am not going to call myself a guru, I do know a lot of stuff regarding PHP. Yet, often find new function pre-defined in most normal installation of PHP that I never knew about. It is for that reason that I am writing this article. In this particular article, I plan on writing information about any PHP functions that I find useful that people may not have heard of before. I will be going through the PHP manual and look at each and every function listed there and put the ones I like here for all of you to look over.
cal_days_in_month
How many of you have written calendar related scripts and have to manually have an array to specifies how many days is in a given month? I bet the anwer is yes for 98% of you. There is a function called cal_days_in_month() that does this for you and it is much more powerful but it can handle multiple type of calendars as well. Yes, I know, you welcome.
debug_backtrace
The function debug_backtrace() was a function I found out awhile back that basically provides an array of what steps were taken to get to its call. This function is very useful for debugging purposes. Likewise, there is also the function debug_print_backtrace() which outputs the backtrace as well.
error_log
Sometimes you may want to add your own custom error messages quickly. The function error_log() allows you to do just that. It is also quite customizable as well. This thing about this though it is that it only sends your error message to the error log but doesn't actually trigger the error. Great for logging possible errors but are of errors that shouldn't prevent the rest of the execution of the script. If you actually want to trigger the error, then the function trigger_error() is for you.
glob
Sometimes you know the pattern of the files you want to retrieve. Instead of getting all the files then filter through them, you can do them all with just a single function call. The function glob() was created for that very reason. Best of all the pattern can be matched against not only files but directories as well.
hash
For the most part, using the function md5() should be good enough. However, there may be times when you want to use a different hashing algorithm. With PHP5, the function hash() was added. You can use it to hash any string using whatever supported algorithm there is. For the list of supported algorithm, just use the function hash_algos()
memory_get_usage
Running a CMS, it is very cruicial that I watch the amount of memory usuage the is required for my site to load a single page. Too much usage can become problem. The function memory_get_usage() allows me to see how much memory my script has taken up.
ob_get_clean
For people that uses output buffering like me, the function ob_get_clean() certainly saves me a small amount of time since I can just use it to get the content of the buffer and stops the output buffering all in one call. Before the discovering of this function, I always had to call the function ob_get_contents() followed by ob_end_clean().
parse_ini_file
Do you don't like storing settings as an XML or JSON but prefers to tried and true INI way? Well, now you can and you don't have to worry about not being able to parse it either. The function parse_ini_file() will do just that. In fact, it is actually quite powerful and I am thinking of maybe switching to use it to store my settings now.
scandir
There are many ways to retrieve a listing of all files and directories under a given directory but none is probably easier than the function scandir(). After all, one function call rather that multiple, including a loop, is much easier to work with.
strtotime
How nice would be to get the unix timestamp is you can just simply give it a string of a given date in plain old regular English? Well, there is a function just for that called strtotime(). Best of all, it can do a whole lot more than that do.