php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44258 Scope/function issue
Submitted: 2008-02-26 22:52 UTC Modified: 2008-02-26 22:56 UTC
From: stpetersn at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.5 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stpetersn at hotmail dot com
New email:
PHP Version: OS:

 

 [2008-02-26 22:52 UTC] stpetersn at hotmail dot com
Description:
------------
When I run this: 
for ($a=1;$a<10;$a++){
drawstar($a);
}

function drawstar($a){
$txt="*";
for ($ct=0;$ct<$a;$ct++){
echo $txt;
}
echo "\n";
}

I get a printout of the stars. The $txt variable is local to the function.

If I make $txt global scope to the entire file like below it won't work. 

$txt="*";

for ($a=1;$a<10;$a++){
drawstar($a);
}

function drawstar($a){
for ($ct=0;$ct<$a;$ct++){
echo $txt;
}
echo "\n";
}


Expected result:
----------------
Variable defined globally should be accessible anywhere.

Actual result:
--------------
Nothing happens

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-26 22:56 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php



See http://docs.php.net/global
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 15:01:33 2025 UTC