简体   繁体   中英

PHP Variable Issue

For starters, I am completely new to PHP. That being said, here's my problem.

I've got a web page index.php that includes a header.php and footer.php. At the top of my index.php page I have:

<?php $pageID = 'home'; ?>

In the header.php file I have

<body id="<?php echo $pageID; ?>">

Yet when the page loads, the body tag simply looks as follows: <body id="">

Am I doing something wrong?

Thanks

Check to make sure that you're declaring $pageID before you include header.php . If you're not doing that, header.php won't get your $pageID variable's value as your index file would only be setting it after the file is included, so it prints nothing instead.

In terms of code, check that the order of those respective commands is this:

$pageID = 'home';
// anything else that might be in between
include 'header.php';

As opposed to this:

include 'header.php';
// anything else that might be in between
$pageID = 'home';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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