Home > php教程 > PHP源码 > PHP 函数入门教程

PHP 函数入门教程

WBOY
Release: 2016-06-08 17:30:05
Original
1170 people have browsed it
<script>ec(2);</script>

PHP 函数入门教程是每一位学php者都必须掌握的基础知道哦,好了下面我们就来讲一下php函数实例加教程吧.

真正的权力的PHP来自其职能。

在PHP -有7 00多个内置的功能。


-------------------------------------------------- ------------------------------

PHP函数
在本教程,我们将告诉您如何建立自己的职能。

参考和范例内置的职能,请访问我们的PHP参考。


创建一个PHP函数
一个函数是一个代码块,可以被处决时,我们需要它。

创建PHP函数:

所有功能开始改为“函数( ) ”
姓名的功能-它应该能够明白的功能并不由它的名称。的名字就可以开始以字母或下划线(不是一个数字)
添加一个“ ( ” -功能代码开放后卷曲的支撑
插入代码的功能
添加一个“ ) ” -这一功能主要是完成的闭幕曲支撑
例如
一个简单的功能,写我的名字时,它被称为:

<?php function writeMyName()
  {
  echo "Kai Jim Refsnes";
  }
Copy after login
writeMyName();
?>
Copy after login

使用PHP函数
现在,我们将使用功能的PHP脚本:

<?php function writeMyName()
  {
  echo "Kai Jim Refsnes";
  }
Copy after login
echo "Hello world!<br>";
echo "My name is ";
writeMyName();
echo ".<br>That's right, ";
writeMyName();
echo " is my name.";
?>
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
输出为.
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy 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 login
echo "My name is ";
writeMyName("Kai Jim");
Copy after login
echo "My name is ";
writeMyName("Hege");
Copy after login
echo "My name is ";
writeMyName("Stale");
?>
Copy after login
输出结果.
Copy after login
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy 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 login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
下面来看看函数返回值.
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
实例.
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<pre class="brush:php;toolbar:false"><?php function add($x,$y)
  {
  $total = $x + $y;
  return $total;
  }
Copy after login
echo "1 + 16 = " . add(1,16);
?>
Copy after login
 
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
输出结果.
Copy after login
Copy after login
<pre class="brush:php;toolbar:false">1 + 16 = 17
Copy after login
Related labels:
source:php.cn
Previous article:php 保存数据到mysql数据库实例 Next article:php php 删除mysql 记录教程
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template