开发者

PHP variables between files

There is a php file (current.php) with some variables, like:

function do() {
    $var = 'something';
}

And one more php开发者_开发百科 file (retrieve.php), which is loaded to current.php with jQuery ajax .load().

The problem is - retrieve.php doesn't see $var.

Tryed this (inside retrieve.php, shows nothing):

global $var;
echo $var;

How to fix?

Thanks.


Some things you must be aware of:

  • When you use PHP, you do not download a file: you execute a script and retrieve its output.
  • PHP variables are destroyed when the script ends. You cannot share variables between two scripts unless you store them somewhere (e.g., in a database or session file).
  • PHP variables are local to the function where you define them, unless you issue a global $foo; statement inside the function.
  • jQuery is a JavaScript library. JavaScript and PHP are different languages: they cannot see each other variables.

Said that, I suggest you reconsider your question and try to explain what you need to accomplish rather that how you want to implement it.


The problem is - retrieve.php doesn't see $var.

Sure it is!
all current.php variables are long dead along with current.php itself, which was run, print some HTML and die.

you have to pass required value using standard HTTP mechanisms. you know - GET, POST etc.


If you want the script you load through AJAX get the value of the vars from the page initiating the AJAX load then you either have to pass the values when loading the AJAX script or store them somewhere temporarily (in DB linked by session ID, or in a session var) so you can retrieve them easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜