vrana Mon Jul 26 11:55:09 2004 EDT
Modified files: /phpdoc/en/language references.xml Log: Referencing global variables inside function (bug #14645) http://cvs.php.net/diff.php/phpdoc/en/language/references.xml?r1=1.28&r2=1.29&ty=u Index: phpdoc/en/language/references.xml diff -u phpdoc/en/language/references.xml:1.28 phpdoc/en/language/references.xml:1.29 --- phpdoc/en/language/references.xml:1.28 Mon Jul 26 11:16:26 2004 +++ phpdoc/en/language/references.xml Mon Jul 26 11:55:09 2004 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.28 $ --> +<!-- $Revision: 1.29 $ --> <chapter id="language.references"> <title>References Explained</title> @@ -81,6 +81,39 @@ Engine and will therefore result in a parser error. </para> </note> + <warning> + <para> + If you assign reference to a variable stated as <literal>global</literal> + inside function, the reference will be visible only inside the function. + You can avoid it by using <varname>$GLOBALS</varname> array. + <example> + <title>Referencing global variables inside function</title> + <programlisting role="php"> +<![CDATA[ +<?php +$var1 = "Example variable"; +$var2 = ""; + +function global_references($use_globals) +{ + global $var1, $var2; + if (!$use_globals) { + $var1 =& $var2; // visible only inside the function + } else { + $GLOBALS["var1"] =& $var2; // visible also in global context + } +} + +global_references(false); +echo "var2 is set to '$var2'\n"; // var2 is set to '' +global_references(true); +echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable' +?> +]]> + </programlisting> + </example> + </para> + </warning> <para> The second thing references do is to pass variables by-reference. This is done by making a local variable in a function and