Functions are a technique for making a script easier to manage, and to save memory space. Actions that are executed more than once within a programme can be efficiently defined and executed by a function, whose name is sugestive of its purpose.
Built-in functions (e.g. isset, echo, order, sort, include) cover to a large degree the needs of a PHP script. However, custom functions can be defined simply to aid the effectiveness of a programme. They are defined by the following syntax:
Function names may include any combination of alphanumeric characters, but the first character must be a letter or the underscore (_).
Custom functions cannot have the same name as built-in functions. Function names are case insensitive, so
newFunction() is the same function as
new_Function() will not call it.
Functions must be defined before they are called, or an
undefined function error will result. It is therefore logical to define functions near the top of a page of script.
Both built-in and custom functions can take arguments (parameters). An argument is information defined by a constant or a variable, which is imported into a function from the script that calls the function.
To call the function with the arguments predefined, the following code appears somewhere in the script:
Arguments may also have a preset default value:
The default argument is now optional when calling the function. If the value is not passed to the function, it will use the default.
A function can return a value:
The scope of a variable is the 'realm' where it has a value and can be accessed. A superglobal variable is always valid. Variables defined on a page are generally valid for that page, but not on another. To pass a variable to another page, the
$_GET method can be used.
Where care must be given is variable scope outside and inside functions. Unless specified with
global $variable;, a variable defined outside a function is not accessible from within the function, and a variable assigned a value within a function does not have this value outside the function.
str_replace() will replace the first string (X) with the second (Y), in the third element ($oldvar).
In this example, the URL brought to a page by the GET function, has underscores (this is because URL uses
%20 in place of spaces, so an underscore is used in the URL title) which need to be replaced by spaces before the
$topic variable is used in the page itself.
mysqli_real_escape_string PHP function escapes special characters in the escapestring so that the modified escapestring may by used in an SQL statement. The escaped characters are: NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
Content © Renewable-Media.com. All rights reserved. Created : August 28, 2014 Last updated :January 12, 2016
The most recent article is:
View this item in the topic:
and many more articles in the subject:
Science resources on ScienceLibrary.info. Games, puzzles, enigmas, internet resources, science fiction and fact, the weird and the wonderful things about the natural world. Have fun while learning Science with ScienceLibrary.info.
1879 - 1968
Otto Hahn, 1879 - 1968, was a German chemist, who was awarded the Nobel Prize for Chemistry in 1944 for his discovery of fission.
"Or I shall have you hung, drawn and decimalled," threatened Napoleon.
"Decimalled?" enquired Sean with a rare mélange of curious anxiety.
"Yes. The French Republic has gone metric," explained Napoleon proudly.
Website © renewable-media.com | Designed by: Andrew Bone