Reply to Confused about scope of globals in included files

Your name:

Reply:


Posted by Wescotte on 02/22/06 20:27

I'm having an issue where what should be global variables are not in
scope and I'm confused as to why.

In Case 1 the variables are not in scope for functions in File2
In Case 2 the varibales are in scope for access in File1's functions
AFTER the include
In Case 3 the varibales are in scope for functionsin File2


Why does case 1 not work but case 3 does? Does case 2 the proper method
to use?

CASE 1:

File1:

switch (OBJ) {
case 1:
include "file2.php"
if Function_In_File2()
....
break;
}

File2:

<?php

$MY_GLOBAL_VARIABLE = "TEST";

Function Function_In_FIle()
{
global $MY_GLOBAL_VARIABLE;
echo $My_GLOBAL_VARIABLE; /* Produces Notice: Undefined variable:
$My_GLOBAL_VARIABLE in File2.php on line xx */
}

?>

CASE 2:


File1:

switch (OBJ) {
case 1:
include "file2.php"
if Function_In_File2()
....
break;
}

File2:

<?php

global $MY_GLOBAL_VARIABLE; $MY_GLOBAL_VARIABLE = "TEST";

Function Function_In_FIle()
{
global $MY_GLOBAL_VARIABLE;
echo $My_GLOBAL_VARIABLE; /* Displays "TEST" */
}

?>

CASE 3:


File1:

switch (OBJ) {
case 1:
include "file2.php"
echo $MY_GLOBAL_VARAIBLE; /* Displays "TEST" Why does this work and
not case 1? */

}

File2:

<?php

$MY_GLOBAL_VARIABLE = "TEST";

?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация