Last month I started writing functions in a particular way, which has made my life as a programmer much easier on more than one occasion. No matter how many comments or verbose parameter names you put in you can end up writing code that you will get lost in. The reason is simple. Lets say you had a function that took in a couple of parameters.
Printing out stuff with PHP is basically essential to any program, but problems can arise if you want to use the same script for command line scripting and web site scripting because of the need to print out HTML. The $_SERVER super global array contains a variable called SERVER_PROTOCOL, which contains the protocol that the client is using to access the script. If the client access through the web then the protocol will contain something like 'HTTP 1.1'. If the script is being run from the command line then this super global variable SERVER_PROTOCOL would exist.
Lots of applications require a user to input a URL and lots of problems occur as a result. I was recently looking for something that would take a URL as an input and allow me to make sure that is was formatted properly. There wasn't anything that did this so I decided to write it myself.
One thing that PHP doesn't allow is the creation of multiple functions with the same name. If you try this then PHP will give you a fatal error. Creating different functions with the same name is useful if you want to pass different parameters to a function that produces the same result. There are two ways around this problem.
The first is that you can pass the function an array of the items that you want. You can then use this array in the function to do whatever you want. For example.
The array_flip() function in PHP is used to swap the values of an array with the keys. Take the following array.