1. Custom function
A function is a block of code that can be executed whenever needed.
Function Declaration:
All functions begin with the keyword "function()"
Name the function - the name of the function should suggest its function. Function names start with a letter or underscore.
Add "{" - The part after the opening curly brace is the code of the function.
Insert function code
Add a "}" - the function ends by closing the curly brace.
Example:
[php] <html> <body> <?php function writeMyName() { echo "jeremyLin"; } writeMyName(); ?> </body> </html>
2. Function - Adding Parameters
Our first function is a very simple one function. It can only output a static string.
We add more functionality to functions by being able to add parameters. A parameter is like a variable.
Example:
[php] <html> <body> <?php function writeMyName($fname) { echo $fname . "jeremy.<br />"; } echo "My name is "; writeMyName("lin"); echo "My name is "; writeMyName("kobe"); echo "My name is "; writeMyName("John"); ?> </body> </html>
3. Function - return value
Functions can also be used to return values.
[php] <html> <body> <?php function add($x,$y) { $total = $x + $y; return $total; } echo "4 + 26 = " . add(4,26); ?> </body> </html>
The output of the above code:
4+26=30
The above is the content of php (7) PHP function. For more related information, please Follow the PHP Chinese website (m.sbmmt.com)!