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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 21:01:33 2024 UTC