7.1. Standard functions
There are more than 1,000 standard functions in the standard php distribution package. These standard functions are built-in to the system and can be used directly without user creation.
like:
<?php
echo md5('123456');
echo '<br/>';
echo sha1('123456');
echo '<br/>';
echo pi();
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2. Custom functions
7.2.1 Basic principles of function naming:
1. The function name cannot be duplicated with the existing function name.
2. Function names can only contain letters, numbers and underscores.
3. Function names cannot start with numbers
7.2.2 Basic use: declare with function
<?php
//Create a function
function funcCountArea($radius)
{
return $radius*$radius*pi();
}
//Use functions
$area = funcCountArea(20);
echo $area;
echo '<br/>';
$area2 = funcCountArea(30);
echo $area2;
?>
output
1256.63706144
2827.43338823
7.2.3 Pass parameters by value
<?php
$a = 5;
function funcChange($a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
5
7.2.4 Pass parameters by reference
<?php
$a = 5;
function funcChange(&$a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
10
7.2.5 Function calls that return multiple values
<?php
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 (practical: recommended)
<?php
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] => Array ( [0] => Mark [1] => 925472 ) )
Note: Function calls are case-insensitive, but variable names are case-sensitive.
7.2.7 Understanding scope:
Local variables:
Variables declared inside the function.
Global variables:
Variables declared outside the function.
7.2.8 Convert local variables into global variables
<?php
$a = 5;
function funcChangeValue()
{
global $a;
$a = 10;
}
funcChangeValue();
echo $a;
?>
output
10
7.2.9 Usage of super global variable $GLOBALR
<?php
$GLOBALS['a'] = 5;
function funcChangeValue()
{
$GLOBALS['a'] = 10;
}
funcChangeValue();
echo $GLOBALS['a'];
?>
Output
10
7.3. File contains
7.3.1 Include can contain the same file multiple times
<?php
include '';
include '';
include '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 There is no difference between include_once and include, but the call will only contain the same file once.
<?php
include_once '';
include_once '';
include_once '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 The require() statement contains and runs the specified file.
<?php
require '';
require '';
require '';
?>
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. However, the same file is not included repeatedly.
<?php
require_once '';
require_once '';
require_once '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
7.3.5 The difference between include and require
If there is other code behind Include, when an error occurs when an error occurs in calling include, the subsequent code will continue to be executed, but requires will not.
Include will give a warning when calling a non-existent file, but will continue to execute the following code.
<?php
include '';
echo('this is ');
?>
output
Warning: include() []: failed to open stream: No such file or directory in D:\AppServ\www\Basic7\ on line 2
Warning: include() []: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in D:\AppServ\www\Basic7\ on line 2
this is
Require will give an error when calling a non-existent file and abort the execution of the code.
<?php
require '';
echo('this is ');
?>
Output
Warning: require() []: failed to open stream: No such file or directory in D:\AppServ\www\Basic7\ on line 2
Fatal error: require() []: Failed opening required '' (include_path='.;C:\php5\pear') in D:\AppServ\www\Basic7\ on line 2
7.4. Magic constants
The so-called magic constant is not a real constant, but a variable with a fixed value according to the occasion.
<?php
echo __FILE__;
echo '<br>';
echo __LINE__;
echo '<br>';
function funcTest()
{
echo __FUNCTION__;
}
funcTest();
?>
output
D:\AppServ\www\Basic7\
5
funcTest
There are more than 1,000 standard functions in the standard php distribution package. These standard functions are built-in to the system and can be used directly without user creation.
like:
Copy the codeThe code is as follows:
<?php
echo md5('123456');
echo '<br/>';
echo sha1('123456');
echo '<br/>';
echo pi();
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2. Custom functions
7.2.1 Basic principles of function naming:
1. The function name cannot be duplicated with the existing function name.
2. Function names can only contain letters, numbers and underscores.
3. Function names cannot start with numbers
7.2.2 Basic use: declare with function
Copy the codeThe code is as follows:
<?php
//Create a function
function funcCountArea($radius)
{
return $radius*$radius*pi();
}
//Use functions
$area = funcCountArea(20);
echo $area;
echo '<br/>';
$area2 = funcCountArea(30);
echo $area2;
?>
output
1256.63706144
2827.43338823
7.2.3 Pass parameters by value
Copy the codeThe code is as follows:
<?php
$a = 5;
function funcChange($a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
5
7.2.4 Pass parameters by reference
Copy the codeThe code is as follows:
<?php
$a = 5;
function funcChange(&$a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
10
7.2.5 Function calls that return multiple values
Copy the codeThe code is as follows:
<?php
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 (practical: recommended)
Copy the codeThe code is as follows:
<?php
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] => Array ( [0] => Mark [1] => 925472 ) )
Note: Function calls are case-insensitive, but variable names are case-sensitive.
7.2.7 Understanding scope:
Local variables:
Variables declared inside the function.
Global variables:
Variables declared outside the function.
7.2.8 Convert local variables into global variables
Copy the codeThe code is as follows:
<?php
$a = 5;
function funcChangeValue()
{
global $a;
$a = 10;
}
funcChangeValue();
echo $a;
?>
output
10
7.2.9 Usage of super global variable $GLOBALR
Copy the codeThe code is as follows:
<?php
$GLOBALS['a'] = 5;
function funcChangeValue()
{
$GLOBALS['a'] = 10;
}
funcChangeValue();
echo $GLOBALS['a'];
?>
Output
10
7.3. File contains
7.3.1 Include can contain the same file multiple times
Copy the codeThe code is as follows:
<?php
include '';
include '';
include '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 There is no difference between include_once and include, but the call will only contain the same file once.
Copy the codeThe code is as follows:
<?php
include_once '';
include_once '';
include_once '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 The require() statement contains and runs the specified file.
Copy the codeThe code is as follows:
<?php
require '';
require '';
require '';
?>
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. However, the same file is not included repeatedly.
Copy the codeThe code is as follows:
<?php
require_once '';
require_once '';
require_once '';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
7.3.5 The difference between include and require
If there is other code behind Include, when an error occurs when an error occurs in calling include, the subsequent code will continue to be executed, but requires will not.
Include will give a warning when calling a non-existent file, but will continue to execute the following code.
Copy the codeThe code is as follows:
<?php
include '';
echo('this is ');
?>
output
Warning: include() []: failed to open stream: No such file or directory in D:\AppServ\www\Basic7\ on line 2
Warning: include() []: Failed opening '' for inclusion (include_path='.;C:\php5\pear') in D:\AppServ\www\Basic7\ on line 2
this is
Require will give an error when calling a non-existent file and abort the execution of the code.
Copy the codeThe code is as follows:
<?php
require '';
echo('this is ');
?>
Output
Warning: require() []: failed to open stream: No such file or directory in D:\AppServ\www\Basic7\ on line 2
Fatal error: require() []: Failed opening required '' (include_path='.;C:\php5\pear') in D:\AppServ\www\Basic7\ on line 2
7.4. Magic constants
name |
describe |
_FILE_ |
Current file name |
_LINE_ |
Current line number |
_FUNCTION_ |
Current function name |
_CLASS_ |
Current class name |
_METHOD_ |
Current method name |
Copy the codeThe code is as follows:
<?php
echo __FILE__;
echo '<br>';
echo __LINE__;
echo '<br>';
function funcTest()
{
echo __FUNCTION__;
}
funcTest();
?>
output
D:\AppServ\www\Basic7\
5
funcTest