Search code examples
phprequirerequire-oncerequire-method

how to pass variables between 2 php pages when the latter called with require()


I am a PHP newbie, and I have this question:

say I have:


a.php:

$a = 'foo' ;
$b = 'baz' ;
require ('b.php') ;

How do I pass variables $a and $b to b.php ? How do I use these variables in b.php ?

thanks a lot !!


Solution

  • Just make sure you call require() after setting the variables, and they should be available in b.php.

    a.php:

    $a = 'foo';
    $b = 'baz';
    require('b.php');
    

    b.php:

    echo 'a: '. $a;
    echo 'b: '. $b;