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:
Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on ScienceLibrary.info.
1889 - 1953
Edwin Hubble, 1889 - 1953, was an American astronomer, who made the physical observations which established the Big Bang Theory and the expansion of the universe in astronomy.
Les sanglots longs
Blessent mon coeur
(The long sobbings of autumn violins
wound my heart with a monotonous languor)
Website © renewable-media.com | Designed by: Andrew Bone