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

匆匆过客 提交于 2019-12-13 03:18:00

问题


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 !!


回答1:


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;



回答2:


You can use these variables straight away in b.php

require(), include(), etc... includes the file in the same scope as the include is made, except for functions/classes that get included in the global scope.

Here's the link to the documentation that explains it nicely : http://php.net/manual/en/function.include.php



来源:https://stackoverflow.com/questions/5046369/how-to-pass-variables-between-2-php-pages-when-the-latter-called-with-require

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!