7.1. Standard functions
There are more than 1000 standard functions in the standard PHP release package, which are built-in and can be used directly without the need for user creation
Such as:
Copy CodeThe code is as follows:
echo MD5 (' 123456 ');
Echo '
';
echo SHA1 (' 123456 ');
Echo '
';
echo Pi ();
?>
Output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2. Custom functions
7.2.1 function naming principles:
1. The function name cannot be the same as the existing function name.
2. Function names can only contain letters, numbers, and underscores.
3. Function names cannot start with a number
7.2.2 Basic use: declaring with function
Copy CodeThe code is as follows:
Create a function
function Funccountarea ($radius)
{
Return $radius * $radius *pi ();
}
Using functions
$area = Funccountarea (20);
Echo $area;
Echo '
';
$area 2 = Funccountarea (30);
echo $area 2;
?>
Output
1256.63706144
2827.43338823
7.2.3 parameter by value
Copy CodeThe code is as follows:
$a = 5;
function Funcchange ($a)
{
$a = 2 * $a;
}
Funcchange ($a);
echo $a;
?>
Output
5
7.2.4 by reference
Copy CodeThe code is as follows:
$a = 5;
Function Funcchange (& $a)
{
$a = 2 * $a;
}
Funcchange ($a);
echo $a;
?>
Output
10
7.2.5 function calls that return multiple values
Copy CodeThe code is as follows:
function Funcuserinfo ($username, $password)
{
$userInfo = Array ($username, $password);
return $userInfo;
}
$arr = Funcuserinfo (' Anllin ', ' 123456 ');
Print_r ($arr);
?>
Output
Array ([0] = Anllin [1] = 123456)
7.2.6 Another function call that returns multiple values (useful: recommended)
Copy CodeThe code is as follows:
function Funcuserinfo ($username, $password)
{
$userInfo [] = $username;
$userInfo [] = $password;
return $userInfo;
}
$arr [] = Funcuserinfo (' Bob ', ' 512655 ');
$arr [] = Funcuserinfo (' John ', ' 458736 ');
$arr [] = Funcuserinfo (' Mark ', ' 925472 ');
Print_r ($arr);
?>
Output
Array ([0] = = Array ([0] = Bob [1] = = 512655) [1] = = Array ([0] = John [1] = 458736) [2] = A Rray ([0] = Mark [1] = 925472))
Note: Function calls are case insensitive, but variable names are case-sensitive.
7.2.7 Understanding Scopes:
Local variables:
A variable declared inside a function.
Global variables:
A variable declared outside the function.
7.2.8 local variable into global variable
Copy CodeThe code is as follows:
$a = 5;
function Funcchangevalue ()
{
Global $a;
$a = 10;
}
Funcchangevalue ();
echo $a;
?>
Output
10
7.2.9 the use of super global variable $GLOBALR
Copy CodeThe code is as follows:
$GLOBALS [' a '] = 5;
function Funcchangevalue ()
{
$GLOBALS [' a '] = 10;
}
Funcchangevalue ();
echo $GLOBALS [' a '];
?>
Output
10
7.3. file contains
The use of 7.3.1 include can contain the same file multiple times
Copy CodeThe code is as follows:
Include ' demo1.php ';
Include ' demo1.php ';
Include ' demo1.php ';
?>
Output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 include_once use and include are no different, but the call will only contain the same file once
Copy CodeThe code is as follows:
Include_once ' demo1.php ';
Include_once ' demo1.php ';
Include_once ' demo1.php ';
?>
Output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
The 7.3.3 require () statement contains and runs the specified file.
Copy CodeThe code is as follows:
Require ' demo1.php ';
Require ' demo1.php ';
Require ' demo1.php ';
?>
Output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
The 7.3.4 require_once () statement contains and runs the specified file during script execution. But the same files are not duplicated.
Copy CodeThe code is as follows:
Require_once ' demo1.php ';
Require_once ' demo1.php ';
Require_once ' demo1.php ';
?>
Output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
The difference between 7.3.5 include and require
If there is another code behind the include, the subsequent code will continue to execute when the include error is called, but require will not.
Include warns you when a nonexistent file is called, but continues to execute the following code.
Copy CodeThe code is as follows:
Include ' demo111.php ';
Echo (' This is demo13.php ');
?>
Output
Warning:include (demo111.php) [function.include]: failed to open stream:no such file or directory in D:\AppServ\www\Basic 7\demo13.php on line 2
Warning:include () [function.include]: Failed opening ' demo111.php ' for inclusion (include_path= '.; C:\php5\pear ') in D:\AppServ\www\Basic7\demo13.php on line 2
This is demo13.php
Require when a nonexistent file is called, an error is given and the execution of the code is aborted.
Copy CodeThe code is as follows:
Require ' demo111.php ';
Echo (' This is demo14.php ');
?>
Output
Warning:require (demo111.php) [function.require]: failed to open stream:no such file or directory in D:\AppServ\www\Basic 7\demo14.php on line 2
Fatal Error:require () [function.require]: Failed opening required ' demo111.php ' (include_path= '); C:\php5\pear ') in D:\AppServ\www\Basic7\demo14.php on line 2
7.4. Magic Constants
Name |
Describe |
_file_ |
Current file name |
_line_ |
Current line number |
_function_ |
Name of current function |
_class_ |
Current class name |
_method_ |
Current method Name |
The so-called Magic constants, not the true constants, but to obtain the fixed value of the variable according to the occasion
Copy CodeThe code is as follows:
Echo __file__;
Echo '
';
Echo __line__;
Echo '
';
function Functest ()
{
Echo __function__;
}
Functest ();
?>
Output
D:\AppServ\www\Basic7\demo15.php
5
Functest
http://www.bkjia.com/PHPjc/324799.html www.bkjia.com true http://www.bkjia.com/PHPjc/324799.html techarticle 7.1. Standard functions standard PHP release package has more than 1000 standard functions, these standard functions are built-in system, do not need to create users can directly use such as: Copy code code as follows ...