PHP functions
In this tutorial, we will explain how to create your own functions.
For reference and examples of built-in functions, visit our PHP Reference Manual.
Create a PHP function
A function is a code block that can be executed when needed.
Create a PHP function:
Function description.
Syntax: return the type function name (type parameter 1, type parameter 2 ....);
Return value: variable type
Function type: for example, database tutorial, network...
Description
Function description
Example
Function example (this column may be omitted)
Reference
The name of the index function. The name of the index function may be omitted in this column)
A simple function outputs my name when it is called:
<Html>
<Body>
<? Php Tutorial
Function writeMyName ()
{
Echo "David Yang ";
}
WriteMyName ();
?>
</Body>
</Html>
Now, we need to use this function in the PHP script:
<Html>
<Body>
<? Php
Function writeMyName ()
{
Echo "David Yang ";
}
Echo "Hello world! <Br/> ";
Echo "My name is ";
WriteMyName ();
Echo ". <br/> That's right ,";
WriteMyName ();
Echo "is my name .";
?>
</Body>
</Html> output of the preceding code:
Hello world!
My name is David Yang.
That's right, David Yang is my name.
Condensation above Let's write a detailed tutorial on php functions
<? Php
/*
* 1. Internal function: PHP can declare the function inside the function.
* The purpose is to call the function internally.
* Used to help external functions complete some sub-functions
*
* 2. Recursive functions: Call your own function name within yourself.
*
* 3. Reuse functions
*
* Require: used for static inclusion
* Include: used for dynamic inclusion
* Require_once: used for static inclusion only once
* Include_once: used for dynamic inclusion, only once
*
* 4. Use of some system functions
* Resource = opendir ("directory name ")
* Readdir (resource)
*
*
*/
// Internal functions
Function score ($ php, $ java, $ dotnet)
{
Function php ($ php)
{
If ($ php> 60)
Return "pass ";
Else
Return "fail ";
}
Function java ($ java)
{
If ($ java> 60)
Return "pass ";
Else
Return "fail ";
}
Function dotnet ($ dotnet)
{
If ($ dotnet> 60)
Return "pass ";
Else
Return "fail www.111cn.net/c5-03 /";
}
$ Total = $ php + $ java + $ dotnet;
$ Agv = $ total/3;
Echo "your php score is {$ php},". php ($ php). "<br> ";
Echo "your java score is {$ java},". java ($ java). "<br> ";
Echo "your dotnet score is {$ dotnet},". dotnet ($ dotnet). "<br> ";
Echo "your total score is: {$ total} <br> ";
Echo "your average score is: {$ agv} <br> ";
}
Score (50, 90, 70 );
// Recursive function
Function demo ($ num)
{
Echo $ num. "<br> ";
If ($ num> 0)
Demo ($ num-1 );
Else
Echo "-------------------------------- <br> ";
Echo $ num. "<br> ";
}
Demo (10 );
Function total ($ dirname, & $ dirnum, & $ filename)
{
$ Dir = opendir ($ dirname );
Readdir ($ dir). "<br> ";
Readdir ($ dir). "<br> ";
While ($ filename = readdir ($ dir ))
{
$ Newfile = $ dirname. "/". $ filename;
Echo $ filename. "<br> ";
If (is_dir ($ filename
}
}
$ Dirnum = 0;
$ Filenum = 0;
Total ("c:/windows", $ dirnum, $ filenum );
Echo "total number of directories:". $ dirnum. "<br> ";
Echo "total number of files:". $ filenum. "<br> ";
?>