How To Create And Use Function In PHP
- Function is a reusable block of code.
- To write a function, code the function keyword followed by name of the function, followed by a set of parentheses.
- within the parentheses of a function, you can code an optional paramater list that contain on or more parameters.
- To code a function that returns data, code the return statement in the body of function. The return statement ends the execution of the function and return the specified value.
- When you call a fucntion, the argument in the argument list must be in the same order as the parameters in the parameter list defined by the function, and they must have compatible data types.
- Example: function avf_of_3($x,$y,$z){ return ($x+$y+$z)/3; }
- How to pass arguments by value and by reference in PHP
- By default, all argument are passed by value to the function that are called. This means that PHP send a copy of argument(the value of original variable) to the function, not the argument itself. As the result, the function can't change the value of orginal argument.
- To allow a function to modify the original argument insted of a copy, You can can code an ampersand(&) in font of the parameter. In that case, The argument is passed by reference. This mean that a reference to the original argument is sent to the function. Then, when the function changes the parameter, it actually changing the original argument.
No comments:
Post a Comment