Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.
The following query will create a random vote of between 50 and 100 for each rateable value.
To change a table name in MySQL you can use the ALTER TABLE command with the parameter RENAME TO. Here is an example of a query that will rename the table "atable" to "newtable".
ALTER TABLE atable RENAME TO newtable;
You could also use the RENAME TABLE command with the same effect.
RENAME TABLE atable TO newtable;
This is also useful for renaming all of the tables in the database, just separate each table rename command with a comma.
Use the following function to change any integer into a string containing the integer value as a Roman Numeral.
When people link to your images from their own site they are essentially using your bandwidth to show images on their site, this is also known as hotlinking.
The simplest way of preventing people from doing this is to add a .htaccess file to only allow locally linked images to be served. This checks the domain that is linking to your images by using the referrer and if the domain does not equal you own site then a different image is served, in this case blank.jpg.
Krumo is an open source plugin for your programs that is designed as a replacement to print_r() and var_dump(). These functions are used by developers (myself included) to find out what the program is doing. The main problem is that if there is a lot of data to look at the page can get a bit busy.
Krumo solves this by simplifying the output into a more readable format. It tells you the format of the array or object item and any other information that it can gain. It also puts the data into a set of clickable sections so that if you are interested in a particular section of output then you can click on it and see only that section.
Cutting a string to a specified length is accomplished with the substr() function. For example, the following string variable, which we will cut to a maximum of 30 characters.
$string = 'This string is too long and will be cut short.';
The substr() function has three parameters. The first is the string, the second is where to start cutting from and the third is the amount of characters to cut to. So to cut to 30 characters we would use the following.
It is possible to find out what functions and variables are available from an object at runtime using the PHP functions get_class_methods() and get_object_vars().
Take the following class called testClass.
The $_SERVER superglobal array contains lots of information about the current page location. You can print this off in full using the following line of code.
echo '<pre>'.print_r($_SERVER, true).'</pre>';
Although this array doesn't have the full URI we can piece together the current URI using bits of the $_SERVER array. The following function does this and returns a full URI.
PHP5 allows you to stop classes being extended or to stop child classes overwriting functions.
The first way to use the final keyword is to stop child classes from overwriting functions when they are created. This can be used to stop an important function from being overwritten. To use the final keyword here just add it to the start of function name.