Returning valuesValues are returned by using the optional return statement. 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. See return for more information.
Use of return
Example #1 Use of return
<?php A function can not return multiple values, but similar results can be obtained by returning an array.
Example #2 Returning an array to get multiple values
<?php To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable:
Example #3 Returning a reference from a function
<?php For more information on references, please check out References Explained. Return type declarationsPHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations. Strict typing also has an effect on return type declarations. In the default weak mode, returned values will be coerced to the correct type if they are not already of that type. In strong mode, the returned value must be of the correct type, otherwise a TypeError will be thrown.
ExamplesExample #4 Basic return type declaration
<?php The above example will output: float(3) Example #5 Strict mode in action
<?php The above example will output: int(3) Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in - on line 5 in -:5 Stack trace: #0 -(9): sum(1, 2.5) #1 {main} thrown in - on line 5 Example #6 Returning an object
<?php The above example will output: object(C)#1 (0) { } |