In this tutorial, you will learn about the PHP Function Return statement & what is the difference between the echo and return.

PHP Function Return Statement

The PHP Function Return statement is used to return any type of data, such as string, number, array, boolean, etc.

Example: –

<?php
function myFunc(){
    $var = 'Hello John';
    //The return keyword is used to return data
    return $var;
}

// Assigning return data into the variable
$returnData = myFunc();
echo $returnData;

/* OR you can also do this
echo myFunc();
*/
?>
Browser Output
Hello John

You can return only one data at a time. You can not return multiple data at a time.

<?php
function myFunc(){
    $var = 'Hello John';
    $var2 = "Hello Jean";
    
    /* Wrong
    return ($var, $var2);
    */

    // Right way
    $data = array($var, $var2);
    return $data;
}

$returnData = myFunc();
echo $returnData[0]."<br>";
echo $returnData[1];
?>
Browser Output
Hello John
Hello Jean

What is the difference between Echo and Return?

The PHP echo statement is used to output one or more strings and the PHP Function Return statement is used to return any type of data, such as string, number, array, boolean, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *