1

It was my understanding that $GLOBALS was a super global and enabled a scope to extend across all pages. I'm a noob to PHP and I have been trying to pass a value from one page to another like so:

A.php - reads

$GLOBALS['A'] = 'Passed A';
echo 'Main says:'.$GLOBALS['A'].'<br />';
disp();

function disp(){

echo 'Function says:'.$GLOBALS['A'].'<br />';
}

b.php reads

<?php

echo 'B says:'.$GLOBALS['A'].'<br />';
$code = disp();

function disp(){

echo 'Function in B says:'.$GLOBALS['A'].'<br />';
}
?>

When a.php is called I get Main says:Passed A Function says:Passed A

when it chains to b.php I get Notice: Undefined index: A in (omitted path) \b.php on line 3 B says:

Notice: Undefined index: A in (omitted path) \b.php on line 8 Function in B says

Am I correct in saying this should work? If not is there a way to send a variable from one page to the next without including them in the same file? Any help greatly appreciated.

asteri
  • 11,402
  • 13
  • 60
  • 84

1 Answers1

0

It is true That $GLOBALS can hold the value and it is available in all scopes throughout a script. Thats why b.php become malfunction.Because $GOLBALS must be pass to b.php in order to get the value from $GLOBAL and you have to catch this with variable.

To pass the value From a page to page you can use the followings

  1. Session (for server-side )
  2. $_POST
  3. cookies(for client-side)
WinHtaikAung
  • 414
  • 2
  • 11