第七章 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
