May 11, 2021 PHP
The true power of PHP comes from its functions.
In PHP, more than 1000 built-in functions are available.
Any valid PHP code can appear inside functions, even other functions and class definitions.
To view the full reference manual and examples of all array functions, please visit our PHP Reference Manual.
In this chapter, we'll show you how to create your own functions.
To execute a script when a page loads, you can put it in a function.
Functions are executed by calling functions.
You can call functions anywhere on the page.
Functions are executed by calling functions.
PHP function guidelines:
The name of the function should indicate its functionality
The function name begins with a letter or underscore (cannot begin with a number)
A simple function that outputs my name when it is called:
Output:
To add more functionality to the function, we can add parameters. Arguments are similar to variables.
The argument is specified in parentheses after the function name.
The following example will output a different name, but the last name is the same:
Output:
The following function has two arguments:
Output:
The PHP function passes information to the function through the argument list, which is a list of expressions with commas as separators. PhP parameters are valued from left to right.
To get the function to return a value, use the return statement.
Output: