note 87134 added to language.variables.scope
| From: | mark at markfiend dot com | Date: | Thu, 20 Nov 2008 16:30:28 +0000 |
| Subject: | note 87134 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146771@lists.php.net to get a copy of this message | ||
I'm slightly confused by another developer's script declaring global variables while
already in the global scope; it won't work!
<?php
global $a;
$a = 10;
echo $a . '-';
function something($input) {
echo $a . '-';
$a = $input * 2;
echo $a . '-';
}
something(3);
echo $a;
?>
Outputs (as I expected)
10--6-10
whereas...
<?php
$a = 10;
echo $a . '-';
function something($input) {
global $a;
echo $a . '-';
$a = $input * 2;
echo $a . '-';
}
something(3);
echo $a;
?>
Outputs (again as expected)
10-10-6-6
----
Server IP: 87.124.35.190
Probable Submitter: 87.224.91.55
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/87134
Del: integrated -- https://master.php.net/note/delete/87134/integrated
Del: useless -- https://master.php.net/note/delete/87134/useless
Del: bad code -- https://master.php.net/note/delete/87134/bad+code
Del: spam -- https://master.php.net/note/delete/87134/spam
Del: non-english -- https://master.php.net/note/delete/87134/non-english
Del: in docs -- https://master.php.net/note/delete/87134/in+docs
Del: other reasons-- https://master.php.net/note/delete/87134
Reject -- https://master.php.net/note/reject/87134
Search -- https://master.php.net/manage/user-notes.php