Reference local variable in a function outside in PHP

Description

The following code shows how to reference local variable in a function outside.

Example


<!DOCTYPE html>//from   w w  w . j  ava  2s  . c  om
<html>
  <body>
    <h1>Understanding variable scope</h1>
    <?php
    
    function helloWithVariables() {
      $hello = "Hello, ";
      $world = "world!";
      return $hello . $world;
    }
    
    echo helloWithVariables() . "<br/>";
    echo "The value of \$hello is: '$hello'<br/>";
    echo "The value of \$world is: '$world'<br/>";
    
    ?>

  </body>
</html>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition