Home Backend Development PHP Tutorial 第七章 php自定义函数实现代码_PHP

第七章 php自定义函数实现代码_PHP

Jun 01, 2016 pm 12:14 PM
function customize

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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

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.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

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.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

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

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

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.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

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.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

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.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

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.

Advanced usage of reference parameters and pointer parameters in C++ functions Advanced usage of reference parameters and pointer parameters in C++ functions Apr 21, 2024 am 09:39 AM

Reference parameters in C++ functions (essentially variable aliases, modifying the reference modifies the original variable) and pointer parameters (storing the memory address of the original variable, modifying the variable by dereferencing the pointer) have different usages when passing and modifying variables. Reference parameters are often used to modify original variables (especially large structures) to avoid copy overhead when passed to constructors or assignment operators. Pointer parameters are used to flexibly point to memory locations, implement dynamic data structures, or pass null pointers to represent optional parameters.

See all articles