PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
//page2.php
<?php
require_once 'globals.inc';
if (isset($GLOBALS['data'])){
echo "was set.";
}else{
echo "was not set.";
}
?>
Strange comment
At the first please assign some value to data
At the second please check register_globals php flag.
At the third use following syntax (to avoid this situdation):
opppss sorry i forgot to assign the value. The $GLOBALS['data'] was initialized in page1.php. Then, I tried to access the value of $GLOBALS['data'] in other php pages.
Oh... If you assign some global value in page1.php it doesn't means it appears in all of your scripts.
It means you can use it inside function, classes etc. but only in page1.php (sure if it's your final script)
Don't mix $GLOBALS and $_SESSION
//clsrecord.php
<?php
require_once 'globals.inc';
class Record
{
public function Record(){
$GLOBALS['data'] = 0987;
}
public function getGlobalData(){
return $GLOBALS['data'];
}
}
?>
//page1.php (revised)
<?php
require_once 'clsrecord.php';
require_once 'globals.inc';
echo '<br /><a href="page2.php">page2</a>';
$rec = new Record();
if (isset($GLOBALS['data'])){
$GLOBALS['data'] = 4567;
}
$gdata = $rec->getGlobalData();
echo $gdata;
/* 4567 is the result. Therefore global $data declared in globals.inc
* is accessible even outside the page1.php if the other php file is
* included in page1.php by using include/require.
*
* On the other hand, It will be different if you call other php file
* by using header().
*/
?>