Hướng dẫn php function return value - hàm trả về giá trị 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. return for more information.

Nội dung chính ShowShow

  • Sử dụng lợi nhuận
  • Làm thế nào để bạn trả về một giá trị trong một hàm PHP?
  • Làm thế nào một giá trị có thể được trả về bởi một hàm?
  • Liệu một hàm phải trả về một giá trị PHP?
  • Loại giá trị nào có thể trở lại chức năng PHP?

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. return is omitted the value null will be returned.

Sử dụng lợi nhuận

Làm thế nào để bạn trả về một giá trị trong một hàm PHP?return

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

Làm thế nào một giá trị có thể được trả về bởi một hàm?

Liệu một hàm phải trả về một giá trị PHP?

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();?>

Loại giá trị nào có thể trở lại chức năng PHP?

Ghi chú::

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

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.

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

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.

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.

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

Để 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:

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.

null0

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

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

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

Để 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.

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

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

Ryan Dot Jentzsch tại Gmail Dot Com ¶ ¶

5 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

rstaveley tại seseit dot com ¶ ¶

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.

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

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

Để 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 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

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

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.

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

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

Để 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:

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

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

Để 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.

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

Ryan Dot Jentzsch tại Gmail Dot Com ¶ ¶

5 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

Làm thế nào để bạn trả về một giá trị trong một hàm PHP?

rstaveley tại seseit dot com ¶ ¶use the return keyword inside of your function. Make sure that the return statement is the last statement inside of your function since PHP will exit the function after the return statement is executed.

Làm thế nào một giá trị có thể được trả về bởi một hàm?

Liệu một hàm phải trả về một giá trị PHP?you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

Liệu một hàm phải trả về một giá trị PHP?

Loại giá trị nào có thể trở lại chức năng PHP?You do not need to return values from functions -> it is completely optional.

Loại giá trị nào có thể trở lại chức năng PHP?

Ghi chú:: Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called.