Maison >php教程 >php手册 >一步一步学习PHP(3) php 函数

一步一步学习PHP(3) php 函数

WBOY
WBOYoriginal
2016-06-13 12:18:52833parcourir

1. 方法概述

首先,写一个最简单的函数,大家看一眼就可以了:

<span style="COLOR: blue"><span style="COLOR: maroon">html</span><span style="COLOR: blue">>
<span style="COLOR: maroon">head</span><span style="COLOR: blue">>
  <span style="COLOR: maroon">title</span><span style="COLOR: blue">></span>HelloPHP<span style="COLOR: blue"></span><span style="COLOR: maroon">title</span><span style="COLOR: blue">>
</span><span style="COLOR: maroon">head</span><span style="COLOR: blue">>
<span style="COLOR: maroon">body</span><span style="COLOR: blue">>
  </span><span style="COLOR: maroon">php
    </span><span style="COLOR: red">function CustomPrint</span>($<span style="COLOR: red">str</span>)
    {
      <span style="COLOR: red">for</span>($<span style="COLOR: red">i</span><span style="COLOR: blue">=0;$i</span>i</span>++)
      {
        <span style="COLOR: red">echo</span>($<span style="COLOR: red">str</span>);
        <span style="COLOR: red">echo</span>(<span style="COLOR: blue">'<br>'</span>);
      }
    }
    
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"Hello"</span>);
  <span style="COLOR: blue">?>
</span><span style="COLOR: maroon">body</span><span style="COLOR: blue">>
</span><span style="COLOR: maroon">html</span><span style="COLOR: blue">>
</span></span></span></span>

通过这个例子,相信大家都了解了PHP中函数的大致写法,至于语法上,和其他类C语言差别不大,也都是while,for,if等,至于其他不同之处,会在之后的文章中,逐渐来说。

OK,那我来总结一下,这个方法的要点:

A. PHP的方法用function来声明,这一点类似于我们熟悉的Javascript.

B. 在使用变量必须要以美元符($)来开头。

2. 参数的引用传递和值传递

参数的值传递和引用传递,相信每个人在学习C语言时都接触到了,在此用C#来写例子:

<span style="COLOR: blue">public void </span>Swap(<span style="COLOR: blue">int </span>a, <span style="COLOR: blue">int </span>b)
{
  <span style="COLOR: blue">int </span>temp = a;
  a = b;
  b = temp;
}
<span style="COLOR: blue">public void </span>Swap(<span style="COLOR: blue">ref int </span>a, <span style="COLOR: blue">ref int </span>b)
{
  <span style="COLOR: blue">int </span>temp = a;
  a = b;
  b = temp;
}

那在这里就写一个PHP版本。

  <span style="COLOR: blue"></span><span style="COLOR: maroon">php
    </span><span style="COLOR: red">function Swap1</span>($<span style="COLOR: red">a</span>,$<span style="COLOR: red">b</span>)
    {
      $<span style="COLOR: red">temp</span><span style="COLOR: blue">=$a;
      </span>$<span style="COLOR: red">a</span><span style="COLOR: blue">=$b;
      </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=$temp;
    </span>}
    <span style="COLOR: red">function Swap2</span>(&$<span style="COLOR: red">a</span>,&$<span style="COLOR: red">b</span>)
    {
      $<span style="COLOR: red">temp</span><span style="COLOR: blue">=$a;
      </span>$<span style="COLOR: red">a</span><span style="COLOR: blue">=$b;
      </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=$temp;
    </span>}
    
    <span style="COLOR: red">function CustomPrint</span>($<span style="COLOR: red">str</span>)
    {
      <span style="COLOR: red">echo</span>($<span style="COLOR: red">str</span>);
      <span style="COLOR: red">echo</span>(<span style="COLOR: blue">"<br>"</span>);  
    }
    
    $<span style="COLOR: red">a</span><span style="COLOR: blue">=1;
    </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=2;
    </span><span style="COLOR: red">Swap1</span>($<span style="COLOR: red">a</span>,$<span style="COLOR: red">b</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"值传递的结果:"</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$a='</span>.$<span style="COLOR: red">a</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$b='</span>.$<span style="COLOR: red">b</span>);
    $<span style="COLOR: red">a</span><span style="COLOR: blue">=1;
    </span>$<span style="COLOR: red">b</span><span style="COLOR: blue">=2;
    </span><span style="COLOR: red">Swap2</span>($<span style="COLOR: red">a</span>,$<span style="COLOR: red">b</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">"引用传递的结果:"</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$a='</span>.$<span style="COLOR: red">a</span>);
    <span style="COLOR: red">CustomPrint</span>(<span style="COLOR: blue">'$b='</span>.$<span style="COLOR: red">b</span>);
  <span style="COLOR: blue">?>
</span>

在这个例子中:有两点我需要说明:

A. 值传递和引用传递的区别在于在参数前的“&”.

B. CustomPrint('$a='.$a);在这句中,需要特殊说明一下单引号和双引号的区别,他们之间只有一个区别,就是能否解析变量名,这个例子就足够说明问题了:

  <span style="COLOR: blue"></span><span style="COLOR: maroon">php 
    </span>$<span style="COLOR: red">a</span><span style="COLOR: blue">=1;
    </span><span style="COLOR: red">echo</span>(<span style="COLOR: blue">"$a"</span>);
    <span style="COLOR: red">echo</span>(<span style="COLOR: blue">"<br>"</span>);
    <span style="COLOR: red">echo</span>(<span style="COLOR: blue">'$a'</span>);
  <span style="COLOR: blue">?>
</span>

 

 

最后,说下关于性能的问题,在按值传递时,PHP需要进行复制,然后再传递,这样如果那些大对象或者字符串的话,就会不仅仅耗费时间,而且对空间也是一种浪费。这时,如果进行引用传递,就免去了耗费性能的复制操作。对性能提高很有好处。

3. 作用域问题

在C#中,由于变量在使用之前必须声明,因此会涉及到一个作用域和子作用域的概念,而在PHP中则没有这样的概念。

我们来看一段C#代码:

<span style="COLOR: blue">public class </span><span style="COLOR: #2b91af">Student
</span>{
  <span style="COLOR: blue">private string </span>name;
  <span style="COLOR: blue">public void </span>SayHello()
  {
    <span style="COLOR: #2b91af">HttpContext</span>.Current.Response.Write(<span style="COLOR: #a31515">"Hello,I am " </span>+ name);
  }
}

也就是说,在方法内可以访问外部类声明的变量,但是在PHP中则不一样:

  <span style="COLOR: blue"></span><span style="COLOR: maroon">php 
    </span>$<span style="COLOR: red">name</span><span style="COLOR: blue">="kym"</span>;
    <span style="COLOR: red">function SayHello</span>()
    {
      <span style="COLOR: red">if</span>(<span style="COLOR: red">isset</span>($<span style="COLOR: red">name</span>))
      {
        <span style="COLOR: red">echo</span>(<span style="COLOR: blue">"Hello $name"</span>);  
      }
      <span style="COLOR: red">else
      </span>{
        <span style="COLOR: red">echo</span>(<span style="COLOR: blue">'$name is undefined'</span>);
      }
    }
    <span style="COLOR: red">SayHello</span>();
  <span style="COLOR: blue">?>
</span>

这里说明一个函数“isset”,这个函数可以检测一个变量是否被定义,或者是否是空字符串。

那么这个结果说明,在函数体内,无法访问到外部变量$name。

在这里在多提一点:一个与unset对应的函数:unset。该函数用于移除一个变量的值。

写个简单的例子:

<span style="COLOR: red"><?php </SPAN><span style="COLOR: #660000">$name</span>=<span style="COLOR: #008200">"kym"</span>;
  <span style="COLOR: blue">if</span>(<span style="COLOR: blue">isset</span>(<span style="COLOR: #660000">$name</span>))
  {
    <span style="COLOR: blue">echo</span>(<span style="COLOR: #008200">"Yes"</span>);
  }
  <span style="COLOR: blue">else
  </span>{
    <span style="COLOR: blue">echo</span>(<span style="COLOR: #008200">"No"</span>);
  }
  <span style="COLOR: blue">unset</span>(<span style="COLOR: #660000">$name</span>);
  <span style="COLOR: blue">if</span>(<span style="COLOR: blue">isset</span>(<span style="COLOR: #660000">$name</span>))
  {
    <span style="COLOR: blue">echo</span>(<span style="COLOR: #008200">"Yes"</span>);
  }
  <span style="COLOR: blue">else
  </span>{
    <span style="COLOR: blue">echo</span>(<span style="COLOR: #008200">"No"</span>);
  }
<span style="COLOR: red">?></span></span>

 

关于这些会在之后的垃圾回收里详细提及。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn