The real power of PHP comes from its functions.
In PHP, more than 1000 built-in functions are provided.
For a complete reference manual and examples of all array functions, visit our PHP Reference manual.
In this chapter, we’ll show you how to create your own functions.
To execute a script when the page loads, you can put it in a function.
A function is executed by calling a function.
You can call functions from anywhere on the page.
Creating PHP functions Functions are performed by calling functions.
grammar
PHP function guidelines:
The function name should start with a letter or underscore (not a number). Example of a simple function that prints my name when called:
The instance
Output:
To add more functionality to a function, we can add parameters, which are like variables.
The parameters are specified in parentheses following the function name.
Example 1 The following instance will print a different name, but with the same last name:
The instance
Output:
My name is Kai Jim Refsnes. My sister’s name is Hege Refsnes. My brother’s name is Stale Refsnes. Example 2 The following function takes two arguments:
The instance
Output:
My name is Kai Jim Refsnes. My sister’s name is Hege Refsnes! My brother’s name is Stale Refsnes? To tell a function to return a value, use the return statement.
The instance
Output:
1 plus 16 is 17
Source code free download address: github.crmeb.net/u/defu