Hướng dẫn do php functions need return? - các hàm php có cần trả về không?

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

<?php
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ị

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

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

<?php
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:

<?php
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.

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

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

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

BGalloway tại CityCarshare Dot org ¶

14 năm trước

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

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

Nick tại itomic.com

19 năm trước

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

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

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

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

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

K-Gun !! thư ¶

5 năm trước

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

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

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

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

rstaveley tại seseit dot com ¶

12 năm trước

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

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

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

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

Ryan Dot Jentzsch tại Gmail Dot Com ¶

5 năm trước

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

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

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

rstaveley tại seseit dot com ¶

12 năm trước

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

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

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

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

BGalloway tại CityCarshare Dot org ¶

14 năm trước

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

Nick tại itomic.com

19 năm trước

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

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

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

K-Gun !! thư ¶

Một số chức năng không trả về một giá trị quan trọng, nhưng những chức năng khác thì có.Điều quan trọng là phải hiểu giá trị của chúng là gì, làm thế nào để sử dụng chúng trong mã của bạn và cách tạo các hàm trả về các giá trị hữu ích.. It's important to understand what their values are, how to use them in your code, and how to make functions return useful values.

Các chức năng có cần trả lại không?

Bất kể các chức năng của bạn là bao lâu và phức tạp, bất kỳ chức năng nào không có câu lệnh trả về rõ ràng hoặc một chức năng trả lại mà không có giá trị trả về, sẽ không trả về.any function without an explicit return statement, or one with a return statement without a return value, will return None .