Ok, here's some code that's based on mine. It's not the same for space-sake.

<?
$maxchars = 40;

function yada( $one, $two, $three )
{
	$temp = $one+$two;

$final = $maxchars;

for( $i = 0; $i < $final; $i++ )
{
	print "$temp<br>";
}
}

if( $something )
{
	$one = 1;
	$two = 3;
	$three = 2;

yada( $one, $two, $three );
}
?>

Why can I not access the value of $maxchars inside of the function? From what I know if you declare it in a higher scope, it should be able to access it, right?

I've seen and tried this as well:

global $maxchars = 40;
global $maxchars;
$maxchars = 40;
GLOBAL $maxchars;
$maxchars = 40;

Any ideas? I can give further clarification if needed.

    "From what I know if you declare it in a higher scope, it should be able to access it, right?"

    Generally, yes.

    However, the global scope (in the usual sense) does not extend implicitly to functions, you have to explicitly declare with global or use $GLOBALS, though I think the global keyword is preferable.

      Well, i did try the 'global' or 'GLOBAL' words to specify that it was a global variable, but it still couldn't access it.

      What is this $GLOBALS you speak of and how do you use it?

        In your example:

        <?
        $maxchars = 40;
        
        function yada( $one, $two, $three )
        {
        	global $maxchars;
        
        $temp = $one+$two;
        
        $final = $maxchars;
        
        for( $i = 0; $i < $final; $i++ )
        {
            print "$temp<br>";
        }
        }

        Though it would make more sense to use $maxchars right away instead of going through $final

          Ahhhh, I see the light now, thank you!

          The reason why I traded it off like that is because in the actual script is quite a bit more complicated (several functions use the global variable) and i wanted to make the example as close to the real thing as possible.

            no problem 🙂

            you probably should have searched the PHP manual though, especially since you were on the right track but just needed that bit of info.

              Write a Reply...