Gitesh Sawariya Gitesh Sawariya - 2 months ago 7
PHP Question

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'));
?>

Answer

I believe you must want this line

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

to actually be this:

$path = $template . '.php';