PHP,从另一个 php 文件获取变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13135131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:50:03  来源:igfitidea点击:

PHP, getting variable from another php-file

php

提问by Carl Papworth

So I wonder if it is possible to get a variable from a specific php-file when the variable-name is used in multiple php-file. An example is this:

所以我想知道在多个 php 文件中使用变量名时是否可以从特定的 php 文件中获取变量。一个例子是这样的:

<header>
 <title>
  <?php echo $var1; ?>
 </title>
</header>

page1.php has $var1 = 'page1'page2.php has $var1 = 'page2'

page1.php 有$var1 = 'page1'page2.php 有$var1 = 'page2'

footer.php should have <a href="">$var1 from page1</a><a href="">$var1 from page2</a>

footer.php 应该有 <a href="">$var1 from page1</a><a href="">$var1 from page2</a>

Ok the example is a bit abstract, but as short as I can make it. I think you get what I am getting at! So it is the in the footer I am after! Got any solutions?

好的,这个例子有点抽象,但我能做到的尽可能短。我想你明白我的意思!所以它是我所追求的页脚!有什么解决办法吗?

回答by Wayne Whitty

You can, but the variable in your last include will overwrite the variable in your first one:

可以,但是最后一个包含中的变量将覆盖第一个中的变量:

myfile.php

我的文件

$var = 'test';

mysecondfile.php

我的第二个文件

$var = 'tester';

test.php

测试文件

include 'myfile.php';
echo $var;

include 'mysecondfile.php';
echo $var;

Output:

输出:

test

tester

测试

测试员

I suggest using different variable names.

我建议使用不同的变量名。

回答by Chris

You could also use a session for passing small bits of info. You will need to have session_start(); at the top of the PHP pages that use the session else the variables will not be accessable

您还可以使用会话来传递少量信息。你需要 session_start(); 在使用会话的 PHP 页面顶部,否则变量将无法访问

page1.php

页面1.php

<?php

   session_start();
   $_SESSION['superhero'] = "batman";

?>
<a href="page2.php" title="">Go to the other page</a>

page2.php

页面2.php

<?php 

   session_start(); // this NEEDS TO BE AT THE TOP of the page before any output etc
   echo $_SESSION['superhero'];

?>

回答by Mohit Yadav

using include 'page1.php'in second page is one option but it can generate warnings and errors of undefined variables.
Three methods by which you can use variables of one php file in another php file:

include 'page1.php'在第二页中使用是一种选择,但它会产生未定义变量的警告和错误。
您可以在另一个 php 文件中使用一个 php 文件的变量的三种方法:

  • use session to pass variable from one page to another
    method:
    first you have to start the session in both the files using php command

    sesssion_start();
    then in first file consider you have one variable
    $x='var1';

    now assign value of $x to a session variable using this:
    $_SESSION['var']=$x;
    now getting value in any another php file:
    $y=$_SESSION['var'];//$y is any declared variable

  • using get method and getting variables on clicking a link
    method

    <a href="page2.php?variable1=value1&variable2=value2">clickme</a>
    getting values in page2.php file by $_GET function:
    $x=$_GET['variable1'];//value1 be stored in $x
    $y=$_GET['variable2'];//vale2 be stored in $y

  • if you want to pass variable value using button then u can use it by following method:

    $x='value1'
    <input type="submit" name='btn1' value='.$x.'/>
    in second php
    $var=$_POST['btn1'];

  • 使用会话将变量从一个页面传递到另一种
    方法:
    首先,您必须使用 php 命令在两个文件中启动会话

    session_start();
    然后在第一个文件中考虑你有一个变量
    $x='var1';

    现在使用此将 $x 的值分配给会话变量:
    $_SESSION['var']=$x;
    现在在任何另一个 php 文件中获取值:
    $y=$_SESSION['var'];//$y 是任何声明的变量

  • 使用 get 方法并在单击链接方法时获取变量

    <a href="page2.php?variable1=value1&variable2=value2">clickme</a>
    通过 $_GET 函数获取 page2.php 文件中的值:
    $x=$_GET['variable1'];//value1 be stored in $x
    $y=$_GET['variable2'];//vale2 be stored in $y

  • 如果您想使用按钮传递变量值,那么您可以通过以下方法使用它:

    $x='value1'
    <input type="submit" name='btn1' value='.$x.'/>
    在第二个 php
    $var=$_POST['btn1'];

回答by dewaz

You could also use file_get_contents

你也可以使用file_get_contents

 $url_a="http://127.0.0.1/get_value.php?line=a&shift=1&tgl=2017-01-01";
 $data_a=file_get_contents($url_a);

 echo $data_a;