• Tutorial List

PHP Function Return Statement & difference between Echo vs Return

Last updated May 15, 2020
PHP Function Return Statement & difference between Echo vs 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.

<?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.