The real power of PHP comes from its functions.
In PHP, more than 700 built-in functions are available.
PHP functions
In this tutorial, we will explain how to create your own functions.
Creating PHP Functions
A function is a block of code that can be executed at any time when it is needed.
To Create a PHP function :
All functions use the keyword "function ()" to start
Named function-the name of the function should indicate its function. The function name begins with a letter or underscore.
Add "{"-the section after the opening curly brace is the code of the function.
inserting function code
Add a "}"-the function ends by closing the curly braces.
Example
A simple function that can output my name when it is called:
Using PHP functions
Now, we're going to use this function in our PHP script:
"; echo" My name is "; Writemyname (); echo". That's right, "; Writemyname (); echo" is my name. ";? > |
The output of the above code:
Hello world! My name is David Yang.that's right and David Yang is my name. |
PHP functions-Adding parameters
Our first function is a very simple function. It can only output a static string.
With the ability to add parameters, we add more functionality to the function. The parameter is similar to a variable.
You may notice that the function name is followed by a parenthesis, such as Writemyname (). The parameters are specified in parentheses.
Example 1
The following example tells the output a different name, but the last name is the same:
"; } echo "My name is"; Writemyname ("David"), echo "My name is", Writemyname ("Mike"), echo "My name is"; Writemyname ("John"); > |
The output of the above code:
My name is David yang.my name was Mike yang.my name is John Yang. |
Example 2
The following function has two parameters:
"; } echo "My name is"; Writemyname ("David", "."); echo "My name is"; Writemyname ("Mike", "!"); echo "My name is"; Writemyname ("John", "..."); > |
The output of the above code:
My name is David yang.my name is Mike yang! My name is John Yang ... |
PHP Functions-return values
Functions can also be used to return values.
Example
The output of the above code:
http://www.bkjia.com/PHPjc/446729.html www.bkjia.com true http://www.bkjia.com/PHPjc/446729.html techarticle the real power of PHP comes from its functions. In PHP, more than 700 built-in functions are available. PHP functions in this tutorial, we will explain how to create your own functions. ...