With regard to the whole "global vars are evil arguement," I would agree to a certain point.
For things such as "$is_logged_in," then yes, I agree that global vars are evil. On the other hand, if you look at phpBB's code, the use global vars for internal variables such as "global $db." In this case, $db is a database abstraction layer that that entire project uses. For example:
PHP Code:
/* simply authenticating users with plain text is bad in my opinion, but this is just an example*/
function auth_user(&$username, &$password)
{
global $db;
$query = "SELECT blah, blah.....";
$result = $db->sql_query($query);
$auth_array = $db->sql_fetchrow($result);
/* more code to process the results, but not important to this example */
}
For something like this, the $db class is used throughout the script, and it's easier than having to constantly pass the $db class to a function. Apparently there are problems with passing objects to a function, and using global vars in this way can help augment that.
Associative Array Interpolation in Strings
PHP Code:
echo $array['key']; // is better than
echo $array[key];
However, if you try to use $array['key'] in a double quotes echo, it won't work. What you have to do is this:
PHP Code:
echo "This is my var in the array [COLOR=red]{[/COLOR]$array['key'][COLOR=red]}[/COLOR].";
The braces help the parser distinguish the quotes between the brackets, i.e. [].
Bookmarks