PHP Conference Japan 2024

返回值

使用可選的 return 陳述式來回傳值。可以回傳任何類型,包括陣列和物件。這會導致函式立即結束執行,並將控制權交還給呼叫它的程式碼行。詳情請參閱 return

注意:

如果省略 return,則會回傳 null 值。

return 的使用

範例 #1 return 的使用

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

函式無法返回多個值,但可以透過返回一個陣列來達到類似的效果。

範例 #2 返回一個陣列以取得多個值

<?php
function small_numbers()
{
return [
0, 1, 2];
}
// 陣列解構會分別收集陣列中的每個成員
[$zero, $one, $two] = small_numbers();

// 在 7.1.0 之前,唯一的替代方案是使用 list() 結構
list($zero, $one, $two) = small_numbers();

?>

要從函式返回參考,請在函式宣告和將返回值賦值給變數時使用參考運算子 &。

範例 #3 從函式返回參考

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

$newref =& returns_reference();
?>

關於參考的更多資訊,請參考參考說明

新增註記

使用者貢獻的註記 4 則註記

rstaveley at seseit dot com
14 年前
具有 C 語言背景的開發人員可能會期望陣列的傳遞方式是傳址 (pass by reference)。 然而,陣列和純量一樣,都是以傳值 (pass by value) 的方式傳遞,這可能會令人感到意外。 物件則是以隱式傳址的方式傳遞。

<?php

# (1) Objects are always passed by reference and returned by reference

class Obj {
public
$x;
}

function
obj_inc_x($obj) {
$obj->x++;
return
$obj;
}

$obj = new Obj();
$obj->x = 1;

$obj2 = obj_inc_x($obj);
obj_inc_x($obj2);

print
$obj->x . ', ' . $obj2->x . "\n";

# (2) Scalars are not passed by reference or returned as such

function scalar_inc_x($x) {
$x++;
return
$x;
}

$x = 1;

$x2 = scalar_inc_x($x);
scalar_inc_x($x2);

print
$x . ', ' . $x2 . "\n";

# (3) You have to force pass by reference and return by reference on scalars

function &scalar_ref_inc_x(&$x) {
$x++;
return
$x;
}

$x = 1;

$x2 =& scalar_ref_inc_x($x); # Need reference here as well as the function sig
scalar_ref_inc_x($x2);

print
$x . ', ' . $x2 . "\n";

# (4) Arrays use pass by value sematics just like scalars

function array_inc_x($array) {
$array{'x'}++;
return
$array;
}

$array = array();
$array['x'] = 1;

$array2 = array_inc_x($array);
array_inc_x($array2);

print
$array['x'] . ', ' . $array2['x'] . "\n";

# (5) You have to force pass by reference and return by reference on arrays

function &array_ref_inc_x(&$array) {
$array{'x'}++;
return
$array;
}

$array = array();
$array['x'] = 1;

$array2 =& array_ref_inc_x($array); # Need reference here as well as the function sig
array_ref_inc_x($array2);

print
$array['x'] . ', ' . $array2['x'] . "\n";
ryan dot jentzsch at gmail dot com
7 年前
PHP 7.1 允許在類型宣告前加上 ? 來表示 void 和 null 返回類型(例如,function canReturnNullorString(): ?string)

然而,不允許使用 resource 作為返回類型

<?php
函式 fileOpen(字串 $fileName, 字串 $mode): 資源
{
$handle = fopen($fileName, $mode);
if (
$handle !== false)
{
return
$handle;
}
}

$resourceHandle = fileOpen("myfile.txt", "r");
?>

錯誤訊息:
致命錯誤:未捕獲的 TypeError:fileOpen() 的返回值必須是資源的實例,返回了資源。
bgalloway at citycarshare dot org
16 年前
在 return 行中使用「執行此操作或 die()」邏輯時要小心。它的運作方式與您的預期不同。

<?php
函式 myfunc1() {
return(
'thingy' or die('otherthingy'));
}
函式
myfunc2() {
return
'thingy' or die('otherthingy');
}
函式
myfunc3() {
return(
'thingy') or die('otherthingy');
}
函式
myfunc4() {
return
'thingy' or 'otherthingy';
}
函式
myfunc5() {
$x = 'thingy' or 'otherthingy'; return $x;
}
echo
myfunc1(). "\n". myfunc2(). "\n". myfunc3(). "\n". myfunc4(). "\n". myfunc5(). "\n";
?>

只有 myfunc5() 返回 'thingy' - 其餘返回 1。
nick at itomic.com
21 年前
返回引用的函式可能會返回 NULL 值。這與透過引用傳遞的函式參數不能作為 NULL(或者實際上任何不是變數的東西)傳遞的事實不一致。

例如:

<?php

函式 &testRet()
{
return
NULL;
}

if (
testRet() === NULL)
{
echo
"NULL";
}
?>

解析正常並顯示 NULL
To Top