A Function is a peace of code of large program.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.
Besides the built-in PHP functions, we can create our own functions.
A user defined function declaration starts with the word "function":
In the example below, we create a function named "writeMsg()". The function outputs "Hello world!". To call the function, just write its name:
Return "world" from the string:
Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:
Using the start parameter with different positive and negative numbers:
The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
Using the start parameter with different positive and negative numbers:
To let a function return a value, use the return statement:
Using the start parameter with different positive and negative numbers: