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

前端之家收集整理的这篇文章主要介绍了第七章 php自定义函数实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

7.1.标准函数
标准PHP发行包中有1000多个标准函数,这些标准函数都是系统内置的,不需要用户创建就可以直接使用
如:
<div class="codetitle"><a style="CURSOR: pointer" data="11060" class="copybut" id="copybut11060" onclick="doCopy('code11060')"> 代码如下:

<div class="codebody" id="code11060">
<?PHP
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进行声明
<div class="codetitle"><a style="CURSOR: pointer" data="2774" class="copybut" id="copybut2774" onclick="doCopy('code2774')"> 代码如下:
<div class="codebody" id="code2774">
<?PHP
//创建函数 
function funcCountArea($radius)
{
return $radius$radiuspi();
}
//使用函数
$area = funcCountArea(20);
echo $area;
echo '
';
$area2 = funcCountArea(30);
echo $area2;
?>

output
1256.63706144
2827.43338823
7.2.3按值传参
<div class="codetitle"><a style="CURSOR: pointer" data="80748" class="copybut" id="copybut80748" onclick="doCopy('code80748')"> 代码如下:
<div class="codebody" id="code80748">
<?PHP
$a = 5;
function funcChange($a)
{
$a = 2 $a;
}
funcChange ($a);
echo $a;
?>

output
5
7.2.4按引用传参
<div class="codetitle"><a style="CURSOR: pointer" data="20269" class="copybut" id="copybut20269" onclick="doCopy('code20269')"> 代码如下:
<div class="codebody" id="code20269">
<?PHP
$a = 5;
function funcChange(&$a)
{
$a = 2
$a;
}
funcChange ($a);
echo $a;
?>

output
10
7.2.5返回多个值的函数调用
<div class="codetitle"><a style="CURSOR: pointer" data="1706" class="copybut" id="copybut1706" onclick="doCopy('code1706')"> 代码如下:<div class="codebody" id="code1706">
<?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另一种返回多个值的函数调用(实用:推荐)
<div class="codetitle"><a style="CURSOR: pointer" data="83952" class="copybut" id="copybut83952" onclick="doCopy('code83952')"> 代码如下:<div class="codebody" id="code83952">
<?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 ) )
注意:函数调用是不区分大小写的,但是变量名是区分大小写的。
7.2.7理解作用域:
局部变量:
函数内部声明的变量。
全局变量
函数外部声明的变量。
7.2.8局部变量转换成全局变量
<div class="codetitle"><a style="CURSOR: pointer" data="10656" class="copybut" id="copybut10656" onclick="doCopy('code10656')"> 代码如下:<div class="codebody" id="code10656">
<?PHP
$a = 5;
function funcChangeValue()
{
global $a;
$a = 10;
}
funcChangeValue();
echo $a;
?>

output
10
7.2.9超级全局变量$GLOBALR的使用
<div class="codetitle"><a style="CURSOR: pointer" data="30546" class="copybut" id="copybut30546" onclick="doCopy('code30546')"> 代码如下:<div class="codebody" id="code30546">
<?PHP
$GLOBALS['a'] = 5;
function funcChangeValue()
{
$GLOBALS['a'] = 10;
}
funcChangeValue();
echo $GLOBALS['a'];
?>

Output
10
7.3.文件包含
7.3.1 Include的使用,可以包含相同的文件多次
<div class="codetitle"><a style="CURSOR: pointer" data="97434" class="copybut" id="copybut97434" onclick="doCopy('code97434')"> 代码如下:<div class="codebody" id="code97434">
<?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使用上和include没什么区别,但是调用多次只会包含相同的文件一次
<div class="codetitle"><a style="CURSOR: pointer" data="96770" class="copybut" id="copybut96770" onclick="doCopy('code96770')"> 代码如下:<div class="codebody" id="code96770">
<?PHP
include_once 'demo1.PHP';
include_once 'demo1.PHP';
include_once 'demo1.PHP';
?>

output
e10adc3949ba59abbe56e057f20f883e
7c4a8d09ca3762af61e59520943dc26494f8941b
3.14159265359
7.3.3 require() 语句包含并运行指定文件
<div class="codetitle"><a style="CURSOR: pointer" data="72203" class="copybut" id="copybut72203" onclick="doCopy('code72203')"> 代码如下:<div class="codebody" id="code72203">
<?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 require_once() 语句在脚本执行期间包含并运行指定文件.但是不重复包含相同的文件
<div class="codetitle"><a style="CURSOR: pointer" data="45904" class="copybut" id="copybut45904" onclick="doCopy('code45904')"> 代码如下:<div class="codebody" id="code45904">
<?PHP
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在调用一个不存在的文件时,会给出警告,但是会继续执行后面的代码
<div class="codetitle"><a style="CURSOR: pointer" data="98243" class="copybut" id="copybut98243" onclick="doCopy('code98243')"> 代码如下:<div class="codebody" id="code98243">
<?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
Require在调用一个不存在的文件时,会给出一个错误,并中止代码的执行。
<div class="codetitle"><a style="CURSOR: pointer" data="41227" class="copybut" id="copybut41227" onclick="doCopy('code41227')"> 代码如下:<div class="codebody" id="code41227">
<?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.魔法常量

名称

描述

_FILE_

当前文件

_LINE_

当前行号

_FUNCTION_

当前函数

_CLASS_

当前类名

_METHOD_

当前方法

所谓的魔法常量,并不是真的常量,而是根据场合去获取固定值的变量
代码如下:
PHP
echo __FILE__;
echo '
';
echo __LINE__;
echo '
';
function funcTest()
{
echo __FUNCTION__;
}
funcTest();
?>

output
D:\AppServ\www\Basic7\demo15.PHP
5
funcTest 原文链接:https://www.f2er.com/php/27427.html

猜你在找的PHP相关文章