Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What is the error in this program?

<html>

<head>

<title>Local Scope</title>

</head>

<body>

<?php

$a = 5; //global scope

function myTest()

{

echo $a; //local scope

}

myTest();

?>

</body>

</html>

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    The script above will not produce any output because the echo statement refers to the local scope variable $a, which has not been assigned a value within this scope.

    You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

    Local variables are deleted as soon as the function is completed.

    Try this instead:

    <html>

    <head>

    <title>Local Scope</title>

    </head>

    <body>

    <?php

    function myTest()

    {

    $a = 5;

    echo $a; //local scope

    }

    myTest();

    ?>

    </body>

    </html>

    For queries, email or IM me.

    Source(s): Web Developer/Designer.
  • 9 years ago

    To access a global variable from within a function, use the global keyword:

    <?php

    $a = 5;

    function myTest()

    {

    global $a;

    echo $a;

    }

    myTest();

    ?>

    The script above will output 5.

    For more visit: http://php-addiction.blogspot.in/2012/09/php-varia...

  • 7 years ago

    access a global variable from within a function, use the global keyword:

    <?php

    $a = 5;

    function myTest()

    {

    global $a;

    echo $a;

    }

    myTest();

    ?>

    The script above will output 5.

Still have questions? Get your answers by asking now.