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 © Andrew Bone. 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:
Physics is the science of the very small and the very large. Learn about Isaac Newton, who gave us the laws of motion and optics, and Albert Einstein, who explained the relativity of all things, as well as catch up on all the latest news about Physics, on ScienceLibrary.info.
1858 - 1937
Sir Jagadish Chandra Bose was an Indian physicist who worked in the field of radio and plant science, and is considered one of India's greatest scientist.
We are like tennant famers chopping down the fence around our house for fuel when we should be using Nature's inexhaustible sources of energy - sun, wind and tide. I'd put my money on the sun and solar energy. What a source of power! I hope we don't have to wait until oil and coal run out before we tackle that.
Website © contentwizard.ch | Designed by Andrew Bone