Write Down Function 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.<?phpsin(3.1415);?>
- Define your own functionsSooner 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.<? Phpfunction?>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.<? Phpfunction 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).<? Phpfunction 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 }.<? Phpfunction 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.<? Phpfunction 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 parametersIn 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.<? Phpfunction myFunction ($ x, $ test = 'foobar') {echo 'x has the value: "' $ x.. '" test and has the value: "' $ test.. '"';}myFunction (4, 'word') / / outputs 4 and word ofmyFunction (5) / / outputs 5 and foobarmyFunction () / / not possible since at least one parameter is requiredmyFunction (1, 2, 3) / / possible, but the third parameter is lost?>4 Return value of functionsAlthough 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.<? Phpfunction myFunction () {do_that () / / is still runningreturn 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.<? Phpfunction myFunction () {/ / Code andererereturn;}myFunction () / / works without problemsMy $ var = function () / / works, $ var got the value NULL (a special data type)?>5 Documentation of own functionsFunctions 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;}?> Function in PHP by NiPS Institute Best Coaching Institute in New Delhi
Function in PHP by NiPS Institute Best Coaching Institute in New Delhi
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment