Broad Network


PHP Return Construct

Foreword: In this tutorial, I talk about the PHP return statement. You should have read the previous series, before reaching here, as this is the continuation.

By: Chrysanthus Date Published: 17 Nov 2018

Introduction

In this tutorial, I talk about the PHP return statement. You should have read the previous series, before reaching here, as this is the continuation.

The Function Structure
A function is of the form:

    function functionName (type1 ident1, type2 ident2, …)
        {
            //statements
            return value; // optional
        }

The return statement is optional, and is usually the last statement, in many situations. In its absence, NULL is returned.

The return Statement
Values are returned 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.

The return statement itself can simply be:

    return;

In this case nothing is returned; that is, null is returned.

The return statement can be:

    return value;

Here, value, can be the actual datum, e.g, a number such as, 56. It can also be any data type.

Meaning of the return Statement
The return statement means, stop executing the function just after the return statement and return to the caller (calling function). The following program illustrates this:

<?php

    function fn()
        {
            echo 1, "<br>";
            echo 2, "<br>";
            return 56;
            echo 3, "<br>";
            echo 4, "<br>";
        }

     $var = fn();
     echo$var;

?>

The output is:

    1
    2
    56

Try the code. Execution of the function definition, fn(), stops at the return of 56. The statements that follow in the function definition are not executed. The calling function is, “fn();”. The called function is the definition of fn().

The return Statement and the if-structure in a Function
You can decide whether or not the statements below the return statement in a function, are executed, using the if-structure. In the next two scripts, the return statement is inside the block of an if-structure, in the function.

In the following program, the statements below the if-structure are executed because the if-condition is false (read and test it).

<?php

    $flt = 2.5;

    function fn()
        {
            global $flt;

            echo 1, "<br>";
            echo 2, "<br>";
            if ($flt == 8.7)
                {
                    return 56;
                }
            echo 3, "<br>";
            echo 4, "<br>";
        }

    fn();

?>

The output is:

    1
    2
    3
    4

In the above function, fn() there is no effective return statement.

In the following program the statements below the if-block are not executed because the if-condition is true (read and test it):

<?php

    $flt = 2.5;

    function fn()
        {
            global $flt;

            echo  1, "<br>";
            echo 2, "<br>";
            if ($flt == 2.5)
                {
                    return 56;
                }
            echo 3, "<br>";
            echo 4, "<br>";
       }

    fn();

?>

The output is:

    1
    2

The return value, 56 was not received by any variable for the fn() function call; it does not have to.

Returning from outside the Function
A function does not only have to return what has been created inside the function definition. It can also return what has been created outside the function. The following program illustrates this, with the fn() function:

<?php

    $flt = 2.5;

    function fn()
    {
        global $flt;

        #some statements
        return $flt;
    }

    $var = fn();
    print $var;

?>

The output is

    2.5

return in Script File
If you want a script file to stop execution, before it reaches its end, place a return statement, where you want it to stop. Do not place the return statement inside a function definition.

If you want an included file to stop execution, before it reaches its end, place a return statement, where you want it to stop. The included file will stop execution and send the return value to the main script. Do not place the return statement inside a function definition in the included file.

Caution with the return Statement
Note that since return is a language construct and not a function, the parentheses surrounding its argument are not required and their use is discouraged.

Note: If no parameter is supplied, then the parentheses must be omitted and NULL will be returned automatically. Calling return with parentheses but with no arguments will result in a parse error (fatal error).

That is it for this part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

Comments