PHP基础(005)---函数

代码一:(函数必须在其调用之前定义)

<?php

$makefoo = true;

/* 我们不能在处调用foo()函数,
   因为它还不存在,但可以调用bar()函数。*/

bar();

if ($makefoo) {
  function foo()
  {
    echo "I don't exist until program execution reaches me. ";
  }
}

echo "<br/>";

/* 现在我们可以安全调用函数 foo()了,
   因为 $makefoo 值为真 */

if ($makefoo) foo();

function bar()
{
  echo "I exist immediately upon program start. "; 
}

?> 
output :(注意输出的先后顺序)
I exist immediately upon program start. 
I don't exist until program execution reaches me. 
View Code

 代码二:(函数中的函数---

PHP 中的所有函数和类都具有全局作用域,可以在内部定义外部调用,反之亦然。

PHP 不支持函数重载,也不可能取消定义或者重定义已声明的函数。)

 1 <?php
 2 
 3 function foo()
 4 {
 5   function bar()
 6   {
 7     echo "I don't exist until foo() is called. ";
 8   }
 9 }
10 
11 /* 现在还不能调用bar()函数,因为它还不存在 */
12 
13 foo();
14 
15 /* 现在可以调用bar()函数了,因为foo()函数
16    的执行使得bar()函数变为已定义的函数 */
17 
18 bar();
19 
20 ?> 
View Code

代码三:(递归函数)

 1 <?php
 2 function recursion($a)
 3 {
 4     if ($a < 20) {
 5         echo "$a ";
 6         recursion($a + 1);
 7     }
 8 }
 9 
10 recursion(10); //output:10 11 12 13 14 15 16 17 18 19 
11 ?>
View Code

代码四:(向函数传递数组)

1 <?php
2 function takes_array($input)
3 {
4     echo "$input[0] + $input[1] = ", $input[0]+$input[1];
5 }
6 $arr=array(2,4);
7 takes_array($arr); //output : 2 + 4 = 6 
8 ?>
View Code

代码五:(用引用传递函数参数:&)

1 <?php
2 function add_some_extra(&$string)
3 {
4     $string .= 'and something extra.';
5 }
6 $str = 'This is a string, ';
7 add_some_extra($str);
8 echo $str;    // outputs 'This is a string, and something extra.'
9 ?> 
View Code

对函数的引用:

 1 <?php
 2     //对函数的引用(在函数名前加一个"&"引用符)
 3     header("Content-type: text/html; charset=utf-8");
 4     function &example($t=0)
 5     {
 6         return $t;
 7     }
 8     $str = &example("好呀!");
 9     echo $str."<p>"; //输出"好呀!"
10 
11 ?>
View Code

代码六:(在函数中使用默认参数)

<?php
function makecoffee($type = "cappuccino")
{
    return "Making a cup of $type. ";
}
echo makecoffee();
echo "<br/>";
echo makecoffee(null);
echo "<br/>";
echo makecoffee("espresso");

?> 
output:
Making a cup of cappuccino. 
Making a cup of . 
Making a cup of espresso. 
View Code

代码七:(多个参数时,函数默认参数正确的用法)

<?php
function makeyogurt($flavour, $type = "abc")
{
    return "Making a bowl of $type $flavour. ";
}

echo makeyogurt("xyz");   // works as expected
?>
output:(注意默认参数的位置)
Making a bowl of abc xyz. 
View Code

代码八:(函数返回值:return)

1 <?php
2 function square($num)
3 {
4     return $num * $num;
5 }
6 echo square(4);   // outputs '16'.
7 ?> 
View Code

代码九:( 可变函数:

PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。可变函数可以用来实现包括回调函数,函数表在内的一些用途。

变量函数不能用于语言结构,例如 echo()print()unset()isset()empty()include()require() 以及类似的语句。需要使用自己的包装函数来将这些结构用作变量函数。)

 1 <?php
 2 function foo() {
 3     echo "In foo()<br /> ";
 4 }
 5 
 6 function bar($arg = '') {
 7     echo "In bar(); argument was '$arg'.<br /> ";
 8 }
 9 
10 // 使用 echo 的包装函数
11 function echoit($string)
12 {
13     echo $string;
14 }
15 
16 $func = 'foo'; //与函数名同名
17 $func();        // output:In foo()
18 
19 $func = 'bar'; //与函数名同名
20 $func('test');  // output:In bar(); argument was 'test'.
21 
22 $func = 'echoit';//与函数名同名
23 $func('test');  // output:test 
24 ?>
View Code

代码十:(可变方法范例***)

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar!";
    }
}

$foo = new Foo();
$funcname = "Variable"; //这里用单引号或双引号是一样的效果
$foo->$funcname();   // This calls $foo->Variable()

?> 
//同函数名
output:This is Bar! 
View Code

 代码十一:(匿名函数,也叫闭包函数(closures))

 匿名函数只在PHP 5.3.0 及以上版本有效!!!

<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
    return strtoupper($match[1]);
}, 'hello-world');
// output: helloWorld
?>

 

 

 

posted on 2014-04-27 01:22  lbsf  阅读(194)  评论(0)    收藏  举报

导航