当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1558 ::
收藏到网摘: n/a

The real power of PHP comes from its functions.
PHP的强大功能主要体现在函数里。

In PHP - there are more than 700 built-in functions available.
PHP中共包含700多个可用的内置函数。


PHP Functions
PHP函数

In this tutorial we will show you how to create your own functions.
在这个教程中我们将告诉你如何创建自己的函数

For a reference and examples of the built-in functions, please visit our PHP Reference.
如果你想了解更多关于PHP参数和内置函数的例子,请访问PHP参数


Create a PHP Function
创建一个PHP函数

A function is a block of code that can be executed whenever we need it.
函数实际上是一个统一的代码块,你可以随时调用它。

Creating PHP functions:
创建PHP函数的方法:

  • All functions start with the word "function()"
    所有函数的开头必须加上“function()”
  • Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
    给函数命名:最好的命名方法是,函数拥有的名称和它所表现的功能相一致。名字可以包含字母或下划线(不可以包含数字)。
  • Add a "{"  - The function code starts after the opening curly brace
    加上一个“{”:函数的代码必须写在“{”符号之后。
  • Insert the function code
    插入一段函数代码
  • Add a "}"  - The function is finished by a closing curly brace
    加上一个“}”:函数书写完毕后,必须加上“}”符号。

Example
案例

A simple function that writes my name when it is called:
下面是一个简单的PHP函数案例,当我们调用它时,它可以输出我们的名字:

<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>

 


Use a PHP Function
使用一个PHP函数

Now we will use the function in a PHP script:
下面我们将使用PHP脚本程序书写函数:

<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>

The output of the code above will be:
上述代码将会输出下面的结果:

Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.

 


PHP Functions - Adding parameters
PHP函数——添加参数

Our first function (writeMyName()) is a very simple function. It only writes a static string.
我们的第一个函数(writeOurName())是一个非常简单的函数,它只是书写了一段静态的字符串。

To add more functionality to a function, we can add parameters. A parameter is just like a variable.
如果你需要给函数加入更多的功能,你可以继续向函数中添加参数。参数就类似于我们说的变量。

You may have noticed the parentheses after the function name, like: writeMyName(). The parameters are specified inside the parentheses.
我们已经注意到,函数的名字后面都加上了一个圆括号“()”;我们要添加的参数就是在圆括号“()”内指定的。

Example 1
案例1

The following example will write different first names, but the same last name:
下面的例子中,输出名字的不同,姓相同:

<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>

The output of the code above will be:
上述代码输出的结果:

My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.

Example 2
案例2

The following function has two parameters:
下面的函数中包含两个参数:

<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Ståle","...");
?>
</body>

</html>

The output of the code above will be:
上述代码输出的结果:

My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Ståle Refsnes...

 


PHP Functions - Return values
PHP函数——返回值

Functions can also be used to return values.
函数也可以用作返回“值[value]”:

Example
案例

<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>

The output of the code above will be:
上述代码将输出下面的结果:

1 + 16 = 17

评论 (1) 1 All

登陆 | 还没注册?