Execute return statement in php. Php program to execute return statement using function. Using this program we can print the return statement using the function. The return statement in PHP 7 now supports Type Declarations.
Enabling the strict requirement, as with the type declaration for function arguments, will result in a “Fatal Error” if a type mismatch occurs. When declaring the function, add a colon ( : ) and the type right before the opening curly ( )bracket to declare a type for the function return.
function addNumbers(float $a, float $b) : float
return $a + $b;
Advantages of Function to Execute Return Statement in PHP
A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. Many functions, such as fopen() and fread(), have already been encountered. Although these are built-in functions, PHP also allows you to construct your own.
Return Statement in PHP
The return keyword terminates a function and, if desired, uses the result of an expression as the function’s return value. When return is used outside of a function, it prevents PHP code from running in the file. The intended data types of the result that a function or a class method should return are specified in the return type declaration.
Declaration in PHP
The declare keyword gives a block of code an execution direction. The directive applies to the rest of the code in the file if the declare statement is not followed by a block. A user-defined function’s declaration begins with the word function, followed by the name of the function you want to write, parenthesis (), and lastly your function’s code enclosed in curly brackets. Using this program we can execute the execute return statement in php.