NewCod3r
NewCod3r

Reputation: 1258

PHP Undefined variable in echo function

I have this code for index.php:

<!DOCTYPE html>
<html>

<?PHP 

if(isset($_GET['page'])){

    require dirname(__FILE__).'/modules/'.$_GET['page'].'/main.php';

} else {

    require dirname(__FILE__).'/modules/home.php';

}

?>
</html>

main.php:

$title = 'title test';
$description = 'desc test';
$keyword = 'keys test';

echo _is_header_();

header function:

function _is_header_(){

     require ABSPATH.'/templates/'.TEMPLATENAME.'/header.php';

}

In header.php I have meta html tag for title and echo $title for show title of page. but I see this error :

<b>Notice</b>:  Undefined variable: title in

how do fix this error?!

NOTE: when I replace require ABSPATH.'/templates/'.TEMPLATENAME.'/header.php'; with echo _is_header_(); my code worked true and show my title.

Upvotes: 0

Views: 769

Answers (2)

Mackson
Mackson

Reputation: 26

If your file "header.php" manipulates the variable "$title" or her not exists, then do you have prints her first, before to include this file.

Upvotes: 0

Paladin
Paladin

Reputation: 1637

In your script

ABSPATH.'/templates/'.TEMPLATENAME.'/header.php';

write a

global $title;

just befor the line you use it first time.

Upvotes: 1

Related Questions