Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.
Sometimes you will want to get a random value form an array in a biased random way, that is, you will want certain values to be returned more than others. Here is a function that will generate a single key from an array, with a greater change of a higher value being retrieved.
Here is a very simple function that will generate a string of random characters, ideal if you want to create a password for a new user.
Spam is a problem. You want to allow people who genuinely want to get in touch to see your email address, but doing this invariably leads to you getting thousands of spam emails.
One solution is to hide your email address in an image, but it can be a pain to create an image for every email address you need. A better solution is to use the PHP GD functions to create an image at runtime so that your email address is displayed, but is completely unreadable to spammers.
To to this you will need to create an image tag on your website, here is an example.
For most functions it is normal for have the function return the output of a calculation. With PHP it is also possible to pass values to the function by reference. A better way of saying this is rather than pass the value of the variable you pass a pointer to the variable itself. When you do this anything that you do to the variable inside the function is also done outside, so if you interact with the variable again it will contain a different value.
The singleton design pattern is used to centralise an object in an application that is used to store changing variables that can then be accessed by other parts of the program. It allows only the single instantiation of an object, hence the name.
The str_word_count() function in PHP does exactly what is says it does. The default of this function is to simply count the number of words present. Take the following string.
$str = "This is a 'string' containing m0re than one word. This is a 'string' containing m0re than one word.";
If we pass this to the str_word_count() function with no other parameters we get the number of words.
Letting the user now that something in the background is working is an essential part of website usability. If nothing at all happens then the user will more than likely either try again or go elsewhere. A good way of doing this is to have a little bit of text that says "Working" and animate dots behind it. Here is a function that will do this.
To redirect the current page to a different location you use the header() function in the following way:
Those of you who have coded in PHP must have looked at the PHP website at some point, even if it was just to down load the latest version, or to look up a function reference.
What most people don't realise is that there is a really easy way to go straight to the page that you want without having to mess around with the tetchy search engine on the PHP site.