PHP 函数入门教程是每一位学php者都必须掌握的基础知道哦,好了下面我们就来讲一下php函数实例加教程吧.
真正的权力的PHP来自其职能。
在PHP -有7 00多个内置的功能。
-------------------------------------------------- ------------------------------
PHP函数
在本教程,我们将告诉您如何建立自己的职能。
参考和范例内置的职能,请访问我们的PHP参考。
创建一个PHP函数
一个函数是一个代码块,可以被处决时,我们需要它。
创建PHP函数:
所有功能开始改为“函数( ) ”
姓名的功能-它应该能够明白的功能并不由它的名称。的名字就可以开始以字母或下划线(不是一个数字)
添加一个“ ( ” -功能代码开放后卷曲的支撑
插入代码的功能
添加一个“ ) ” -这一功能主要是完成的闭幕曲支撑
例如
一个简单的功能,写我的名字时,它被称为:
<?php function writeMyName() { echo "Kai Jim Refsnes"; }
writeMyName(); ?>
使用PHP函数
现在,我们将使用功能的PHP脚本:<?php function writeMyName() { echo "Kai Jim Refsnes"; }Copy after loginecho "Hello world!<br>"; echo "My name is "; writeMyName(); echo ".<br>That's right, "; writeMyName(); echo " is my name."; ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login输出为.Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginHello world! My name is Kai Jim Refsnes. That's right, Kai Jim Refsnes is my nameCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login<div id="result_box" dir="ltr" style="text-align: left"> <p>PHP的函数-添加参数<br>我们的第一个函数( writeMyName ( ) )是一个非常简单的功能。它只是写了一个静态字符串。 <br><br>要添加更多的功能函数,我们可以添加参数。参数就像一个变数。 <br><br>您可能已经注意到括号后的函数名称,如: writeMyName ( ) 。参数所指定的括号内。 <br><br>范例1 <br>下面的例子将第一次写的名字不同,但相同的姓氏:</p> <p> </p> <pre class="brush:php;toolbar:false"><?php function writeMyName($fname) { echo $fname . " Refsnes.<br />"; }Copy after loginecho "My name is "; writeMyName("Kai Jim");Copy after loginecho "My name is "; writeMyName("Hege");Copy after loginecho "My name is "; writeMyName("Stale"); ?>Copy after login输出结果.Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login<pre class="brush:php;toolbar:false">My name is Kai Jim Refsnes. My name is Hege Refsnes. My name is Stale Refsnes.Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login下面来看看函数返回值.Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login实例.Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login<pre class="brush:php;toolbar:false"><?php function add($x,$y) { $total = $x + $y; return $total; }Copy after loginecho "1 + 16 = " . add(1,16); ?>Copy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after loginCopy after login输出结果.Copy after loginCopy after login<pre class="brush:php;toolbar:false">1 + 16 = 17Copy after login