What is return type in php?
In PHP 7, a new feature, Return type declarations has been introduced. Return type declaration specifies the type of value that a function should return. Following types for return types can be declared. Show
Example - Valid Return TypeIt produces the following browser output − 5 Example - Invalid Return TypeIt produces the following browser output − Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned... Type declarations can be added to function arguments, return values, and, as of PHP 7.4.0, class properties. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown.
Single types
Warning Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using
Output of the above example in PHP 8: Warning: "boolean" will be interpreted as a class name. Did you mean "bool"? Write "\boolean" to suppress this warning in /in/9YrUX on line 2 Fatal error: Uncaught TypeError: test(): Argument #1 ($param) must be of type boolean, bool given, called in - on line 3 and defined in -:2 Stack trace: #0 -(3): test(true) #1 {main} thrown in - on line 2 mixedmixed is equivalent to the union type object|resource|array|string|int|float|bool|null. Available as of PHP 8.0.0. ExamplesExample #1 Basic class type declaration
f(C $c) { Output of the above example in PHP 8: C D Fatal error: Uncaught TypeError: f(): Argument #1 ($c) must be of type C, E given, called in /in/gLonb on line 14 and defined in /in/gLonb:8 Stack trace: #0 -(14): f(Object(E)) #1 {main} thrown in - on line 8 Example #2 Basic interface type declaration
f(I $i) { Output of the above example in PHP 8: C Fatal error: Uncaught TypeError: f(): Argument #1 ($i) must be of type I, E given, called in - on line 13 and defined in -:8 Stack trace: #0 -(13): f(Object(E)) #1 {main} thrown in - on line 8 Example #3 Basic return type declaration
The above example will output: Example #4 Returning an object
getC(): C { The above example will output: Nullable type As of PHP 7.1.0, type declarations can be marked nullable by prefixing the type name with a question mark ( Example #5 Nullable argument type declaration
f(?C $c) { The above example will output: Example #6 Nullable return type declaration
Composite typesIt is possible to combine simple types into composite types. PHP allows types to be combined in the following ways:
Caution It is not possible to combine intersection types with union types. Union types A union type declaration accepts values of multiple
different simple types, rather than a single one. Union types are specified using the syntax Nullable union types The Caution false pseudo-type The Caution Caution The Intersection types An intersection type declaration accepts values which satisfies multiple class-type declarations, rather than a single one. Intersection
types are specified using the syntax Duplicate and redundant typesTo catch simple bugs in composite type declarations, redundant types that can be detected without performing class loading will result in a compile-time error. This includes:
For example, if
Return only typesvoid
never never is, in type theory parlance, the bottom type. Meaning it is the subtype of every other type and can replace any other return type during inheritance. static The
value must be an Strict typingBy default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible. For example, a function that is given an int for a parameter that expects a string will get a variable of type string. It is possible to enable strict mode on a per-file basis. In strict mode, only a value corresponding exactly to the type declaration will be accepted, otherwise a TypeError will be thrown. The only exception to this rule is that an int value will pass a float type declaration. Warning
Function calls from within internal functions will not be affected by the To enable strict mode, the
Example #8 Strict typing for arguments values
sum(int $a, int $b) { Output of the above example in PHP 8: int(3) Fatal error: Uncaught TypeError: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 9 and defined in -:4 Stack trace: #0 -(9): sum(1.5, 2.5) #1 {main} thrown in - on line 4 Example #9 Coercive typing for argument values
The above example will output: Example #10 Strict typing for return values
sum($a, $b): int { The above example will output: int(3) Fatal error: Uncaught TypeError: sum(): Return value must be of type int, float returned in -:5 Stack trace: #0 -(9): sum(1, 2.5) #1 {main} thrown in - on line 5 Coercive typing with union types When
If the type both exists in the union, and the value can be coerced to the type under PHPs existing type checking semantics, then the type is chosen. Otherwise the next type is tried. Caution As an exception, if the value is a string and both int and float are part of the union, the preferred type is determined by the existing “numeric string” semantics. For example, for
Example #11 Example of types being coerced into a type part of the union
"45" --> 45 // int numeric string MiscExample #12 Typed pass-by-reference Parameters Declared types of reference parameters are checked on function entry, but not when the function returns, so after the function had returned, the argument's type may have changed.
Output of the above example in PHP 8: int(1) Fatal error: Uncaught TypeError: array_baz(): Argument #1 ($param) must be of type array, int given, called in - on line 9 and defined in -:2 Stack trace: #0 -(9): array_baz(1) #1 {main} thrown in - on line 2 Example #13 Catching TypeError
sum(int $a, int $b) { Output of the above example in PHP 8: int(3) Error: sum(): Argument #1 ($a) must be of type int, float given, called in - on line 10 toinenkayt (ta at ta) [iwonderr] gmail d ¶ 1 year ago
typeArrayNullInt(?int ...$arg): void { anisgazig at example dot com ¶ 1 year ago
crash ¶ 1 year ago
Test implements ITest Hayley Watson ¶ 11 months ago
foo(int|string $arg) manoj904378 at gmail dot com ¶ 10 months ago
What is return type in PHP function?There is no way to define the return type of a function within PHP as it is loosely-typed. You will have to do your validation within the calling function. The bool type was just a example.
How do you define return type?In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method. In many programming languages (especially statically-typed programming languages such as C, C++, Java) the return type must be explicitly specified when declaring a function.
What is void return type in PHP?A void return type has been introduced. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.
What is difference between ECHO and return in PHP?Echo is for display, while return is used to store a value, which may or may not be used for display or other use.
|