note 31566 deleted from language.variables.scope by aidan
| From: | aidan@php.net | Date: | Sun, 04 Jul 2004 02:48:50 +0000 |
| Subject: | note 31566 deleted from language.variables.scope by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72501@lists.php.net to get a copy of this message | ||
Note Submitter: jjh@gmx.at
----
This may seem clear so some, but may be worth mentioning
to others:
The scope of a function differs from the scope of global
variables: Globally defined functions can be accesses from
within other functions without having to be (re)defined using
the "global" keyword!
<?
// Scope of a function:
// This shows that the scope of a globally declared function
// is different from the scope of a global variable.
function MyFunction() {
return "MyFunction says Hello!";
}
$myGlobVar = "The global myGlobVar.";
class A {
function A() {
// myGlobVar has a local scope if not declared global:
$myGlobVar = "myGlobVar in a local scope";
echo "<tr><td>myGlobVar:
"".$myGlobVar.""</td></tr>";
// The global myGlobVar is only known if speciallly declared global:
global $myGlobVar;
echo "<tr><td>myGlobVar:
"".$myGlobVar.""</td></tr>";
// MyFunction() is known without having to be declared global:
$this->a=MyFunction();
echo "<tr><td>this->a:
"".$this->a.""</td></tr>";
echo "</table>";
}
}
echo "<html><head>
<title>Scopes of Variables and Functions</title>
</head><body><table>";
$myA=new A;
echo "</table></body></html>";
?>