是否可以在PHP中回显声明在echo下面的变量?

Mar*_*dal 0 php variables echo

下面的代码不会显示任何输出,因为变量在echo下面声明,因为PHP逐行执行.有没有办法在整个页面中搜索变量然后执行代码?

<?php
include "header.php";
$title = "Test";
?>
Run Code Online (Sandbox Code Playgroud)

header.php

<html>
<head>
<title><? echo $title ?></title>
</head>
Run Code Online (Sandbox Code Playgroud)

Nul*_*teя 5

您需要了解编译器/解释器的工作原理.PHP是解释语言,可以编译用于解释PHP的二进制文件.

PHP从上到下运行.

所以就像

<?php // start from here 

   echo "$title";   <-- $title is undefined here
   $title = "Test"; <-- now you declared $title with value so it goes in  memory now

     //end
Run Code Online (Sandbox Code Playgroud)

因此,您需要首先检查天气$title是否设置,然后根据它进行响应

if(isset($title)){
  echo $title;
}
Run Code Online (Sandbox Code Playgroud)