PHP Magic Method __toString()

During OOP in PHP you need to debug the class, you can use the var_dump method but this isn't so user friendly, so you need an alternative to the var_dump(); PHP have released some magic method which...

Increase The Default Maximum File Upload Size In PHP

If you have a website which allows users to upload their own images then you will need to change the default maximum files size upload. The PHP default for all file uploads is 2MB, so the server will...

Using Static Methods In PHP

A static method allows us to use the method without creating the object first. This means that we can use the scope resolution operator :: to use the method. Static methods can not be accessed by usin...

PHP Calculate Age from Date of Birth

Sometimes you have a persons date of birth and you want to know age of the person. With the below snippet you can send it a date and it will return the age as of today's date. It will first take the...

Force download of files in PHP

If you have files on your website that you want people to download you need to change the code to allow the browser to automatically download the file. This is because if you had an image on your webs...

Convert Seconds to Time in PHP

Here is useful function to convert seconds to a time either years, months, days or hours. function Sec2Time($time){ if(is_numeric($time)){ $value = array( "years" => 0, "days" =&am...

Easily cache pages with PHP

If you are not using a CMS or a framework you may need to cache your pages to speed up the page loading times. This can easily be done in PHP using the ob functions. OB Function In PHP ob_start() ob_...

Remove Whitespace and line breaks in String

In any programming language there is a simple in-built function you can use to remove whitespace from a string the main one used will be a string replace to search for spaces and replace with empty. I...

PHP Delete Directory and Files In Directory

In PHP if you want to delete a directory there is security against deleting a directory which has files in it. Therefore you need to delete all the files in the folder before you can delete the direct...