search
Homephp教程php手册php 函数使用方法与函数定义方法

类似于其他编程语言,PHP的程序员提供了一种方法来定义的功能,它可以在其他地方被称为方案。

对于一个函数的语法是:
函数定义方法
代码如下:
function "function_name" (arg1, arg2...)
{
[code to execute]
return [final_result];
}

其中[final_result]通常是从函数返回变量值。
让我们看一个例子
代码如下:
function double_this_number($input_number)
{
return $input_number*2;
}

调用方法
代码如下:
$x = 10;
$y = double_this_number($x);
print $y;

输出值为
10
好,我们再来看一个复杂一点的函数使用方法
代码如下:
function safePost($v=0)
{
if( $v==0 )
{
$protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS");
foreach($protected as $var) {
if(isset($_REQUEST[$var]) || isset($_FILES[$var]))
{
die("Access denied");
}
}
}
}

调用方法
safePost();
这个可以不定义参数,因为给$v==0默认设置了一个参数,这样对函数的扩展有很好的帮助。
Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools