7. 1. Standard Functions
There are more than 1000 standard functions in the standard php release package. These standard functions are built in the system and can be directly used without user creation.
For example:
Copy codeThe Code is as follows:
<? Php
Echo md5 ('200 ');
Echo '<br/> ';
Echo sha1 ('20140901 ');
Echo '<br/> ';
Echo pi ();
?>
Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
. Custom Functions
7.2.1 basic function naming principles:
1. The function name cannot be the same as the existing function name.
2. The function name can only contain letters, numbers, and underscores.
3. The function name cannot start with a number.
7.2.2 basic usage: declare with function
Copy codeThe Code is as follows:
<? Php
// Create a function
Function funcCountArea ($ radius)
{
Return $ radius * pi ();
}
// Use the Function
$ Area = funcCountArea (20 );
Echo $ area;
Echo '<br/> ';
$ Area2 = funcCountArea (30 );
Echo $ area2;
?>
Output
1256.63706144
2827.43338823
7.2.3 passing parameters by value
Copy codeThe Code is as follows:
<? Php
$ A = 5;
Function funcChange ($)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>
Output
5
7.2.4 passing parameters by reference
Copy codeThe Code is as follows:
<? Php
$ A = 5;
Function funcChange (& $)
{
$ A = 2 * $;
}
FuncChange ($ );
Echo $;
?>
Output
10
7.2.5 function call that returns multiple values
Copy codeThe Code is as follows:
<? Php
Function funcUserInfo ($ username, $ password)
{
$ UserInfo = array ($ username, $ password );
Return $ userInfo;
}
$ Arr = funcUserInfo ('anllin', '20140901 ');
Print_r ($ arr );
?>
Output
Array ([0] => anllin [1] => 123456)
7.2.6 another function call that returns multiple values (practical: recommended)
Copy codeThe Code is as follows:
<? Php
Function funcUserInfo ($ username, $ password)
{
$ UserInfo [] = $ username;
$ UserInfo [] = $ password;
Return $ userInfo;
}
$ Arr [] = funcUserInfo ('bob', '123 ');
$ Arr [] = funcUserInfo ('john', '123 ');
$ Arr [] = funcUserInfo ('mark', '123 ');
Print_r ($ arr );
?>
Output
Array ([0] => Array ([0] => Bob [1] => 512655) [1] = & gt; Array ([0] = & gt; John [1] = & gt; 458736) [2] => Array ([0] => Mark [1] => 925472 ))
Note: function calls are case-insensitive, but variable names are case-sensitive.
7.2.7 understanding scope:
Local variables:
Variable declared in the function.
Global variables:
Variables declared outside the function.
7.2.8 convert local variables to global variables
Copy codeThe Code is as follows:
<? Php
$ A = 5;
Function funcChangeValue ()
{
Global $;
$ A = 10;
}
FuncChangeValue ();
Echo $;
?>
Output
10
7.2.9 use of super global variable $ GLOBALR
Copy codeThe Code is as follows:
<? Php
$ GLOBALS ['a'] = 5;
Function funcChangeValue ()
{
$ GLOBALS ['a'] = 10;
}
FuncChangeValue ();
Echo $ GLOBALS ['a'];
?>
Output
10
7. 3. File Inclusion
7.3.1 use of Include, can contain the same file multiple times
Copy codeThe Code is as follows:
<? Php
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 using include_once is no different from using include, but multiple calls only contain the same file once.
Copy codeThe Code is as follows:
<? Php
Include_once 'demo1. php ';
Include_once 'demo1. php ';
Include_once 'demo1. php ';
?>
Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 the require () statement contains and runs the specified file.
Copy codeThe Code is as follows:
<? Php
Require 'demo1. php ';
Require 'demo1. php ';
Require 'demo1. php ';
?>
Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.4 the require_once () statement contains and runs the specified file during script execution, but does not contain the same file repeatedly.
Copy codeThe Code is as follows:
<? Php
Require_once 'demo1. php ';
Require_once 'demo1. php ';
Require_once 'demo1. php ';
?>
Output
E10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359 s
7.3.5 differences between include and require
If there is other code behind Include, when an error occurs in calling include, the subsequent code will continue to be executed, but require will not.
When calling a non-existent file, Include will give a warning, but will continue to execute the subsequent code.
Copy codeThe Code is as follows:
<? Php
Include 'demo11.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 \ Basic7 \ demo13.php on line 2
Warning: include () [function. include]: Failed opening 'demo111. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in D: \ AppServ \ www \ Basic7 \ demo13.php on line 2
This is demo13.php
When Require calls a non-existent file, it will give an error and abort code execution.
Copy codeThe Code is as follows:
<? Php
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 \ Basic7 \ 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 |
Description |
|
_ FILE _ |
Current file name |
|
_ LINE _ |
Current row number |
|
_ FUNCTION _ |
Current function name |
|
_ CLASS _ |
Current Class Name |
|
_ METHOD _ |
Current Method Name |
The so-called magic constant is not a real constant, but a variable that gets a fixed value based on the occasion.
Copy codeThe Code is as follows:
<? Php
Echo _ FILE __;
Echo '<br> ';
Echo _ LINE __;
Echo '<br> ';
Function funcTest ()
{
Echo _ FUNCTION __;
}
FuncTest ();
?>
Output
D: \ AppServ \ www \ Basic7 \ demo15.php
5
FuncTest