-
Functions in PHP
Besides control and variables are PHP scripts through function calls. If the same code is executing this program code is often outsourced to a function. So instead of having the same code every time you call in the appropriate places on only the function, which then performs the actual work.
The behavior of a function is affected by their parameters. A function can have any number of parameters, including parameters not exactly as an infinite number of parameters. Further, a function return value. This is also the standard use of a function, it will pass parameters and the function returns a value based on this back. For example, the sin function returns the sine of the given radian value.
<?php
sin(3.1415);
?>
-
Define your own functions
Sooner or later we want not only the functionality provided by using special PHP define your own functions. A function definition starts with the keyword function in PHP.
<? Php
function
?>
This is followed by the actual name of the function. The name itself is subject to common rules such as not begin with numbers. Furthermore, the functions have two underscores (__) begin special functionality. You should use this function only when they want exactly the functionality that is described in the manual.
<? Php
function myFunction
?>
Like a function call now follows an open parenthesis (. According to the clamp can now variables for the parameters are given. With the number of variables (comma separated) so you need to decide the number of parameters. However if you are planning a function with any to define many parameters you are at this point no parameters at all and use the functions in the function body func_get_args, func_get_arg and func_num_args. The parameter list is then finished with the closing parenthesis).
<? Php
function myFunction ($ param1, $ param2)
?>
The contents of the function starts with an opening curly brace {. Now you can write any PHP code. At the end of the function body ends with a closing curly chamber }.
<? Php
function myFunction ($ param1, $ param2) {
/ / Here now follows
/ / Normal php code
}
?>
For typical programming style to indent the code within the function by 4 spaces. Thus it can be seen where the function starts (the function keyword) and where it ends (at the closing brace}).
Within the function, the parameter variables can be used. In addition to the superglobal variables no further variables are otherwise available. This is called also variable scope. The variable parameters are then filled in accordance with the values of the function call. If the parameters for the function are variables whose values are passed, but not the variable itself in first place, it is therefore not possible to change variables that are used outside the function.
<? Php
function myFunction () {
echo $ var / / help, where is defined $ var?
}
$ Var = 'HTML';
myFunction () / / will not work because the only function parameters and variables
/ / Super globals are available
?>
3 Optional parameters
In PHP, it is allowed to define which one is not used later in a function call parameters. Thus it is e.g. possible to call the function although theoretically supports 3 parameters a function with only one parameter. So as to define a function assigns default values to the parameters in the parameter list. It is done by specifying an assignment for the parameter. When the function is called with a parameter value that is used for the variable parameters. When the function is called without the parameter as the parameter variable contains the specified default value.
<? Php
function myFunction ($ x, $ test = 'foobar') {
echo 'x has the value: "' $ x.. '" test and has the value: "' $ test.. '"';
}
myFunction (4, 'word') / / outputs 4 and word of
myFunction (5) / / outputs 5 and foobar
myFunction () / / not possible since at least one parameter is required
myFunction (1, 2, 3) / / possible, but the third parameter is lost
?>
4 Return value of functions
Although you use any functions in PHP code, as well as echo, it is customary return a calculated value for a function. This is implemented in PHP with the language construct return. It got to a value or a variable that you want to return.
<? Php
function myFunction () {
do_that () / / is still running
return 300;
mach_das () / / is not running, as was leaving the function / closed
}
echo myFunction () / / outputs 300 from
?>
With the return function is terminated at the same time. This can be done at any point, if it makes sense. In calculating the sine of the value 0 for example be expected not great but there is a case distinction quite early to the value 0 is returned.
It is also possible to exit a function and return with it to indicate no return value. This makes only sense if not expected is that a function has a return value.
<? Php
function myFunction () {
/ / Code anderere
return;
}
myFunction () / / works without problems
My $ var = function () / / works, $ var got the value NULL (a special data type)
?>
5 Documentation of own functions
Functions that are provided by PHP described in detail on the official website. Accordance with specially created features missing such a documentation. You may guess the functionality of the function name. For complex functions are hoping for a good documentation. For this purpose they PHPDocs. Before the actual function to add a PHPDoc comment which describes the function.
<? Php
/ **
* Returns a square number.
*
* This function is passed a parameter and returns
* Square of this function back.
*
* @ Param x The value of the you want to have a perfect square
* @ Return The number of square
* /
function square ($ x) {
return $ x * $ x;
}
?>
Array in php
Array is a special variable in php, it is used to store multiple values in a single varible. I f you want to store a list data, you can store these values in a sigle variable as follows
$car = array("maruthi","BMW","Nizan");
or
$car [0]= "Maruthi";
$car [0]= "BMW";
$car [0]= "Nizan";
array() is used to declare an array in php, There are three types of arrays available in php.Indexed, Associative, Multidimensional are the three types of arrays