So,
just to get this straght, I can use a varaiable inside a function by declaring global and the variable name,
ex:
$name = "joe";
function boldText($some_text)
{
global $name;
print "<b>";
print "$name is a $some_text man";
print "</b>";
}
//call the function
$adject = "big";
print boldText($adject);
//would return --> joe is a big man (with big in bold)
Now, if I want to use $name in another function I will have to make it global again in the new function??
Such as:
function italicText($other_text)
{
global $name;
print "<i>";
print "$name is a $other_text man";
print "</i>";
}
$word = "hairy";
print italicText($word);
//returns --> joe is a hairy man (with hairy in italics)
Kirk Parker wrote:
This is to prevent outer variables from suddenly affecting
a functions inner variables (like a $i in a for loop).
Isn't that putting it backwards? The outer variables can't
affect the inner ones, as (in the absence of multithreading)
the inner loop will have started, run, and finished before
any of the higher-level code will have had a chance to
twiddle with the values of those variables.
The 'global' keyword is rather necessitated by the absence
in PHP of any need to declare variables before using them.
In a language like C, it's the innermost declared variable that
get used, e.g.
int i = 0; // this is global
...
int my_function(int *array, int len)
{
int i; // local variable
for ( i = 0; i < len; i++ ) // it's the local 'i' that gets used here
...
But in PHP, without being able to "declare" a local variable, you have:
$i = 0;
...
function my_fn( $array )
{
for ($i = 0; $i < sizeof($array....