简体   繁体   English

包含header.php的PHP代码无法正常工作

[英]Php code to include header.php not working correctly

I am using these codes so that I can pass the file needed or other attribute through variables but it is not working properly. 我正在使用这些代码,以便可以通过变量传递所需的文件或其他属性,但是它无法正常工作。 I don't know what the problem is because the code looks pretty fine to me. 我不知道问题出在哪里,因为代码对我来说看起来还不错。 Index page does not show any content from header's page. 索引页不显示标题页的任何内容。

helper.php

<?php
function render($template, $data = array())
{
    $path = $template . ' .$php ';
    if(file_exists($path))
    {
        extract($data);
        require($path);
    }
}

header.php

<?php require_once('helper.php') ?>
<!doctype html>
<head>
    <title><?php echo htmlspecialchars($title); ?></title>
</head>
<body>

Index.php

<?php
    require_once('helper.php');
    render('header', array('title' => 'Index'));
?>

This is wrong: 这是错误的:

$path = $template . ' .$php ';

You are adding spaces and a $ sign to your path: 您要在路径中添加空格和$符号:

$path = $template . '.php';

I believe you must want this line 我相信你一定要这条线

$path = $template . ' .$php ';

to actually be this: 实际上是这样的:

$path = $template . '.php';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM