簡體   English   中英

如何在php中更改打印的變量?

[英]How to change printed variable in php?

我在主文件中放置了一個子文件,但是該子文件會影響主文件中的變量。

簡單的例子:

在我的代碼中包含不能在回顯之前。

的index.php

<?php

    $heading= "Heading";

    echo "<h1>$headeing</h1>";

    include_once('specific_data.php');

?>

Specific_data.php

$heading = "Specific Heading";

實際輸出:

<h1>Heading</h1>

所需的輸出:

<h1>Specific Heading</h1>

您在分配新值之前在include os之前回顯該值

嘗試在包含之后移動回顯

 <?php

   $heading= "Heading";



   include_once('specific_data.php');

   echo "<h1>$headeing</h1>";
?>

您可以創建MVC結構

的index.php

include_once 'view.php';

$heading='custom head';

return view('specific_data.php',compact('heading'));


view.php




function view($file,$vars=[])
{

   extract($vars);
   include_once $file.'.php';

}

specific_data.php

<h1>this variable get from index and will render <?=$heading?></h1>

從瀏覽器執行index.php,您將看到以下代碼


this variable get from index and will render custom head

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM