【问题标题】:echo variable with variable in included.phpecho 变量与included.php 中的变量
【发布时间】:2019-07-08 09:59:20
【问题描述】:

我有两个 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 的空值。 用包含文件中的变量回显变量的正确语法是什么?

【问题讨论】:

  • 在第一个文件中,当 $web_id 为空时,您将 $web_id 分配给 $id。因此,存储在 $id 和 $id 中的空值将打印为空。您在 $id 分配之后设置 $web_id,这就是为什么 $id 没有用新的 $web_id 更新的原因。对于您的预期输出,您必须在 $web_id = 'data'; 之后添加 $id=$web_id
  • 您需要在包含之前设置您的$web_id

标签: php variables include echo


【解决方案1】:

您需要在包含之前设置您的$web_id 变量。看看这个例子:

action.php

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

echo $start . $id . $end;

include.php

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

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多