Chapter 7 php custom function implementation code
- 2020-05-10 17:50:48
- OfStack
7.1. Standard functions
The standard php distribution has more than 1000 standard functions that are built into the system and can be used without user creation
Such as:
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2. Custom function
7.2.1 basic principles of function naming:
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 should not start with a number
7.2.2 basic use: declare with function
output
1256.63706144
2827.43338823
7.2.3 pass parameters by value
output
5
7.2.4 pass arguments by reference
output
10
7.2.5 function calls that return multiple values
output
Array ( [0] = > anllin [1] = > 123456 )
7.2.6 another function call that returns multiple values (utility: recommended)
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 understand scope:
Local variables:
A variable declared inside a function.
Global variables:
A variable declared outside a function.
7.2.8 convert local variables to global variables
output
10
7.2.9 use of super global variable $GLOBALR
Output
10
7.3. File contains
7.3.1 the use of Include can contain the same file multiple times
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
--------------------------------------------------------------------------------
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.2 include_once is no different from include in use, but multiple calls only contain the same file once
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 require() statement contains and runs the specified file.
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 the execution of the script. It does not contain the same file repeatedly.
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
7.3.5 differences between include and require
If there is any other code after Include, the code will continue to execute when include is called in error, but require will not.
Include gives a warning when calling a nonexistent file, but continues to execute the following code.
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 inclusion (include_path='.;C:\php5\pear') in D:\AppServ\www\Basic7\demo13.php on line 2
this is demo13.php
When Require calls a nonexistent file, it gives an error and aborts the execution of the code.
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 constant
output
D:\AppServ\www\Basic7\demo15.php
5
funcTest
The standard php distribution has more than 1000 standard functions that are built into the system and can be used without user creation
Such as:
<?php
echo md5('123456');
echo '<br/>';
echo sha1('123456');
echo '<br/>';
echo pi();
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2. Custom function
7.2.1 basic principles of function naming:
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 should not start with a number
7.2.2 basic use: declare with function
<?php
// Create a function
function funcCountArea($radius)
{
return $radius*$radius*pi();
}
// Using the function
$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 arguments 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 (utility: 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 understand scope:
Local variables:
A variable declared inside a function.
Global variables:
A variable declared outside a function.
7.2.8 convert local variables to global variables
<?php
$a = 5;
function funcChangeValue()
{
global $a;
$a = 10;
}
funcChangeValue();
echo $a;
?>
output
10
7.2.9 use 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 the use of Include can contain the same file multiple times
<?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 include_once is no different from include in use, but multiple calls only contain the same file once
<?php
include_once 'demo1.php';
include_once 'demo1.php';
include_once 'demo1.php';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 require() statement contains and runs the specified file.
<?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 the execution of the script. It does not contain the same file repeatedly.
<?php
require_once 'demo1.php';
require_once 'demo1.php';
require_once 'demo1.php';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
7.3.5 differences between include and require
If there is any other code after Include, the code will continue to execute when include is called in error, but require will not.
Include gives a warning when calling a nonexistent file, but continues to execute the following code.
<?php
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\Basic7\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
When Require calls a nonexistent file, it gives an error and aborts the execution of the code.
<?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 constant
The name of the
describe
_FILE_
Current file name
_LINE_
The current line number
_FUNCTION_
Current function name
_CLASS_
The name of the class
_METHOD_
Current method name
A magical constant is not a real constant, but a variable that gets a fixed value depending on the situation
<?php
echo __FILE__;
echo '<br>';
echo __LINE__;
echo '<br>';
function funcTest()
{
echo __FUNCTION__;
}
funcTest();
?>
output
D:\AppServ\www\Basic7\demo15.php
5
funcTest