在include.php中使用变量echo变量

时间:2019-02-14 11:26:38

标签: php variables include echo

我有两个php文件。 一个是action.php,另一个是include.php

我需要在其他脚本中回显可变形式的include.php。

基本上在include.php中有这个变量

     $id= $web_id;
     $start = "Hello, ";
     $end = "works.";

在我的action.php中,我有这个变量

     $web_id = "testing echo from variable in include.php";

     echo $start; // variable is in include.php
     echo $id; //variable is in include.php
     echo $end; //variable is in include.php

结果是

     Hello, works.

我想实现的目标是

     Hello, testing echo from variable in include.php works. 

此语法仅回显$ id的空值。 在包含的文件中用变量回显变量的正确语法是什么?

1 个答案:

答案 0 :(得分:1)

在进行包含之前,您需要设置$web_id变量。看一下这个例子:

action.php

$web_id = 'this'.
include 'include.php';

echo $start . $id . $end;

include.php

$id = $web_id;
$start = 'Hello ';
$end = ' works.';