编程学习网 > PHP技术 > php基础 > PHP基础之函数3——函数返回值
2014
11-07

PHP基础之函数3——函数返回值

值通过使用可选的返回语句返回。可以返回包括数组和对象的任意类型。返回语句会立即中止函数的运行,并且将控制权交回调用该函数的代码行。更多信息见 return

Note:

如果省略了 return,则返回值为 NULL

Example #1 return 的使用

<?php
function square($num)
{
    return $num * $num;
}
echo square(4);   // outputs '16'.
?>

函数不能返回多个值,但可以通过返回一个数组来得到类似的效果。

Example #2 返回一个数组以得到多个返回值

<?php
function small_numbers()
{
    return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?>

从函数返回一个引用,必须在函数声明和指派返回值给一个变量时都使用引用运算符 &:

Example #3 从函数返回一个引用

<?php
function &returns_reference()
{
    return $someref;
}

$newref =& returns_reference();
?>

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取