How to add a function in php: 1. Create a PHP sample file; 2. Create a function through "function functionName(){...}".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to add functions to php?
Creating PHP functions:
Functions are executed by calling functions.
Syntax
<?php function functionName() { // 要执行的代码 } ?>
PHP function guidelines:
The name of the function should indicate its function
The function name starts with a letter or underscore (not a number Beginning)
Example
A simple function that outputs my name when called:
Example
<?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?>
Output:
My name is Kai Jim Refsnes
PHP Function - Adding Parameters
In order to add more functions to the function, we can add parameters, which are similar to variables.
The parameters are specified within a bracket after the function name.
The following example will output different names, but the surname is the same:
Example
<?php function writeName($fname) { echo $fname . " Refsnes.<br>"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?>
Output:
My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.
Recommended learning:《PHP video tutorial》
The above is the detailed content of How to add functions in php. For more information, please follow other related articles on the PHP Chinese website!