Hướng dẫn return a function in php - trả về một hàm trong php

Các giá trị được trả về bằng cách sử dụng câu lệnh trả về tùy chọn. Bất kỳ loại có thể được trả lại, bao gồm các mảng và đối tượng. Điều này làm cho chức năng kết thúc việc thực thi của nó ngay lập tức và chuyển kiểm soát trở lại dòng mà nó được gọi là. Xem trả lại để biết thêm thông tin.return for more information.

Ghi chú::

Nếu lợi nhuận bị bỏ qua, giá trị null sẽ được trả về.return is omitted the value null will be returned.

Sử dụng lợi nhuận

Ví dụ #1 Sử dụng lợi nhuậnreturn

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

Một hàm không thể trả về nhiều giá trị, nhưng có thể thu được kết quả tương tự bằng cách trả về một mảng.

Ví dụ #2 Trả về một mảng để nhận nhiều giá trị

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>

Để trả về một tham chiếu từ một hàm, hãy sử dụng toán tử tham chiếu và trong cả khai báo hàm và khi gán giá trị trả về cho một biến:

Ví dụ #3 Trả về một tham chiếu từ một hàm

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>

Để biết thêm thông tin về tài liệu tham khảo, vui lòng kiểm tra các tài liệu tham khảo được giải thích.

Ryan Dot Jentzsch tại Gmail Dot Com ¶

5 năm trước

PHP 7.1 allows for void and null return types by preceding the type declaration with a ? -- (e.g. function canReturnNullorString(): ?string)

However resource is not allowed as a return type:

function fileOpen(string $fileName, string $mode): resource
{
   
$handle = fopen($fileName, $mode);
    if (
$handle !== false)
    {
        return
$handle;
    }
}
$resourceHandle = fileOpen("myfile.txt", "r");
?>

Errors with:
Fatal error: Uncaught TypeError: Return value of fileOpen() must be an instance of resource, resource returned.

rstaveley tại seseit dot com ¶

12 năm trước

Developers with a C background may expect pass by reference semantics for arrays. It may be surprising that  pass by value is used for arrays just like scalars. Objects are implicitly passed by reference.

# (1) Objects are always passed by reference and returned by referenceclass Obj {
    public
$x;
}

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

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

BGalloway tại CityCarshare Dot org ¶

14 năm trước

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

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

Nick tại itomic.com

19 năm trước

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

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

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

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

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

K-Gun !! thư ¶

5 năm trước

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
0

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
1

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

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
3

rstaveley tại seseit dot com ¶

12 năm trước

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
4

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
5

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
6

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
7

Ryan Dot Jentzsch tại Gmail Dot Com ¶

5 năm trước

function small_numbers()
{
    return [
012];
}
// Array destructuring will collect each member of the array individually
[$zero$one$two] = small_numbers();// Prior to 7.1.0, the only equivalent alternative is using list() construct
list($zero$one$two) = small_numbers();?>
9

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

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
1

rstaveley tại seseit dot com ¶

12 năm trước

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
2

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
3

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
4

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
5

BGalloway tại CityCarshare Dot org ¶

14 năm trước

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
6

Nick tại itomic.com

19 năm trước

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
7

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

function &returns_reference()
{
    return 
$someref;
}
$newref =& returns_reference();
?>
9