RE: [PHP] Problems with globals...
| From: | Boget, Chris | Date: | Tue, 25 Jul 2000 16:58:33 +0000 |
| Subject: | RE: [PHP] Problems with globals... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-8086@lists.php.net to get a copy of this message | ||
> Several problems in the test. The array created just had an
> enumerated index. Also, the results in the "After" section
> were looking at the initial variables, not the stuff you stuck
> into the array and ran through the function.
I knew something was wrong. I just couldn't wrap my brain
around it. It didn't help that the way PHP prints out things
like variable variables is a bit odd (though correct).
> function stringsToLower( $stringsArray ) {
> while ( list($Key,$Val) = each($stringsArray ) ) {
> global $$Key;
> $$Key = strtolower($Val);
> }
> }
> $joe = "This";
> $bob = "That";
> $briggs = "Other";
> echo "Before: $joe $bob $briggs<br>\n";
> $Blah = array( "joe"=>"$joe", "bob"=>"$bob",
> "briggs"=>"$briggs" );
> stringsToLower( $Blah );
> echo "<p>After: $joe $bob $briggs<br>\n";
Still way too much typing, especially if you have lots and lots of
variables you need to convert.
> Things that are different:
> * Created associative index on the array.
Lots of typing. :(
> * Switch to while/list/each rather than a count(), far simpler.
How?
> * "global $$Key;" rather than just "global $Key;"
I tried this one time and it didn't seem to work. I chalk this
up to how PHP prints out variable variables.
> This process seems kind of overblown. I mean, why are you
> making an array to check this? It'd be much simpler to just
> run your variables through the strtolower() directly
As I explained in one of my previous emails, there could be
upwards of 10, 20 or more variables that I would need to do
this to. So I can have 5, 10, 20 or more lines of code doing
nothing but doing this:
> $briggs = strtolower($briggs);
or 1 lines of code. I'd choose the latter, personally. :P
> rather than having to create an array to then check. Perhaps
> the incoming info is already in an array?
No, just an easy way of passing (a widely) variable number of
parameters to a function.
With your working example, I was able to modify my code
thusly and it now works. :)
function stringsToLower( $stringsArray ) {
for( $elNum = 0; $elNum < count( $stringsArray ); $elNum++ ) {
$stringVariable = $stringsArray[$elNum];
global $$stringVariable;
$$stringVariable = strtolower( ${$stringVariable} );
}
}
$joe = "This";
$bob = "That";
$briggs = "Other";
echo "Before: $joe $bob $briggs<br>\n";
stringsToLower( array( "joe", "bob", "briggs" ));
echo "After: $joe $bob $briggs<br>\n";
It doesn't look *that* much different from my original code.
Funny how the slightest little syntax can push things over
the edge to "working". :)
Thanks for your help!
Chris