Home > php教程 > php手册 > body text

php 函数使用方法与函数定义方法_php基础

WBOY
Release: 2016-05-17 09:01:53
Original
1400 people have browsed it

对于一个函数的语法是:
函数定义方法

复制代码 代码如下:

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默认设置了一个参数,这样对函数的扩展有很好的帮助。
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template