Within the later versions of PHP, there are over 7000 built-in functions. A function is a a pre-defined set of actions to perform within PHP and can be used to minimise repetitive procedures such as formatting text in a certain why, handling data or even displaying content.
The following is an example of a function in it’s simplest form:
echo “Simple PHP Function”;
echo “Page Title:”;
The name of the function can be anything as long as it does not use the name of an existing function which will result in a ‘redeclare error. Also, a function should only be set once on a page. I like to server side include my functions:
Now, let’s try something a little more involved:
$file_name = ereg_replace(“[^a-z^A-Z^0-9^ ^-]”, “”, $string); /// Only include alpha-numerics
$file_name = strtolower($file_name); /// Convert all characters to lowercase.
$file_name = substr($file_name, 0, 150); /// shorten long string to maximum 150 characters
$file_name = trim($file_name); /// Remove spaces at end of string.
$file_name = str_replace(” “, “-“, $file_name); /// Replace spaces with ‘-‘ hyphens.
$file_name = $file_name.”.html”; /// Add .html to end of string
return $file_name; /// End execution and return value of function.
$title = “A Simple PHP Function “;
echo turn_title_into_url($title); /// echo function on $title string.
This should output“a-simple-php-function.html”. I’m not sure that this function will be any use to you, but study it’s content to gather together a better concept of how PHP functions work.