note 31566 added to language.variables.scope
| From: | jjh at gmx dot at | Date: | Sat, 26 Apr 2003 10:40:11 +0000 |
| Subject: | note 31566 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-47131@lists.php.net to get a copy of this message | ||
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>";
?>
--
http://www.php.net/manual/en/language.variables.scope.php
http://master.php.net/manage/user-notes.php?action=edit+31566
http://master.php.net/manage/user-notes.php?action=delete+31566
http://master.php.net/manage/user-notes.php?action=reject+31566