第七章 php自定义函数实现代码_php基础
7.1.标准函数
标准php发行包中有1000多个标准函数,这些标准函数都是系统内置的,不需要用户创建就可以直接使用
如:
echo md5('123456');
echo '
';
echo sha1('123456');
echo '
';
echo pi();
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.2.自定义函数
7.2.1函数命名基本原则:
1.函数名不能和已有的函数名重名。
2.函数名只能包含字母、数字和下划线。
3.函数名不能以数字开头
7.2.2基本使用:用function进行声明
//创建函数
function funcCountArea($radius)
{
return $radius*$radius*pi();
}
//使用函数
$area = funcCountArea(20);
echo $area;
echo '
';
$area2 = funcCountArea(30);
echo $area2;
?>
output
1256.63706144
2827.43338823
7.2.3按值传参
$a = 5;
function funcChange($a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
5
7.2.4按引用传参
$a = 5;
function funcChange(&$a)
{
$a = 2 * $a;
}
funcChange ($a);
echo $a;
?>
output
10
7.2.5返回多个值的函数调用
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另一种返回多个值的函数调用(实用:推荐)
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 ) )
注意:函数调用是不区分大小写的,但是变量名是区分大小写的。
7.2.7理解作用域:
局部变量:
在函数内部声明的变量。
全局变量:
在函数外部声明的变量。
7.2.8局部变量转换成全局变量
$a = 5;
function funcChangeValue()
{
global $a;
$a = 10;
}
funcChangeValue();
echo $a;
?>
output
10
7.2.9超级全局变量$GLOBALR的使用
$GLOBALS['a'] = 5;
function funcChangeValue()
{
$GLOBALS['a'] = 10;
}
funcChangeValue();
echo $GLOBALS['a'];
?>
Output
10
7.3.文件包含
7.3.1 Include的使用,可以包含相同的文件多次
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使用上和include没什么区别,但是调用多次只会包含相同的文件一次
include_once 'demo1.php';
include_once 'demo1.php';
include_once 'demo1.php';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 require() 语句包含并运行指定文件。
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 require_once() 语句在脚本执行期间包含并运行指定文件.但是不重复包含相同的文件。
require_once 'demo1.php';
require_once 'demo1.php';
require_once 'demo1.php';
?>
output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359s
7.3.5 include与require的区别
Include后面如果还有其他代码,当调用include出错时,后面的代码还会继续执行,但是require则不会。
Include在调用一个不存在的文件时,会给出警告,但是会继续执行后面的代码。
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
Require在调用一个不存在的文件时,会给出一个错误,并中止代码的执行。
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.魔法常量
名称 |
描述 |
_FILE_ |
当前文件名 |
_LINE_ |
当前行号 |
_FUNCTION_ |
当前函数名 |
_CLASS_ |
当前类名 |
_METHOD_ |
当前方法名 |
echo __FILE__;
echo '
';
echo __LINE__;
echo '
';
function funcTest()
{
echo __FUNCTION__;
}
funcTest();
?>
output
D:\AppServ\www\Basic7\demo15.php
5
funcTest

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
