View Single Post
 
Old March 3rd, 2004, 08:43 PM
nikolai nikolai is offline
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way to think about it is that you only have three scopes -- global scope, class scope, and function scope. Variables created in global scope are accessible anywhere else in global scope.

All HTML and plain text in a PHP script is just automatic output in global scope. All <?php does is enter you into a PHP code block in global scope. The ?> tag puts you back into "text output mode" in global scope.

It's important to see it this way because it explains fundamentally why variables don't cease to exist at the end of a PHP code block -- you're not actually opening or closing scopes. PHP doesn't handle nested scopes within any scope context (i.e. global, class, function).


For example:

<?php

// In C, C++, and Java, the next line would open a new scope:
{
   $foo = "hello, world.";
}

// In C, C++, and Java, $foo wouldn't exist here, because its life
// would be confined to the curly-brace scope block defined above.
// PHP has no such "nested scopes".

echo $foo; // prints "Hello, world."

?>



Take care,

Nik
http://www.bigaction.org/