Well, first things first. Does the error message specifically point to $thisPage as the offender? When and where was its value set? Is the quoted bit of code inside a function?
Where variables are concerned, php behaves the opposite of many other languages: variables are assumed to be local unless you specifically say otherwise.
|I get an undefinded variable message when errors are turned on. |
Does the code still work as you expect? (Are you sure?)
As lucy24 implies... $thisPage is the only variable in the code snippet you have posted and this is indeed not defined (hence the E_NOTICE message)! To be honest, if this variable is not defined when the above code executes then it's quite possible this code is not working "as intended".
Aside: Interesting use of a DL-list to display a list of links. (Why not an unordered list?) Note, however, that HTML5 requires at least one DT element (per DD group) to be valid. (HTML4 allowed the DT to be omitted.)
I was hoping the value of $thisPage comes from
cited earlier. But if so, you should probably either change "include" to "require", or make sure the page code has an escape clause in case $thisPage gets lost. ("If $blahblah is irredeemably empty, then set it to 'xyz'." Or whatever floats your boat.)
Hey, this is a navigation doodad isn't it? "If the item in question refers to the current page, make it look like one thing; otherwise make it look like a different thing." As noted elsewhere, I speak about three words of php-- and this is one of 'em ;) Originally I had pagename data in the same file that built the navigation; it changed to a separate file with "require" when I went to two pieces, header and footer, both using the same information.
|I was hoping the value of $thisPage comes from |
True, one would hope. However, I was assuming that the code snippet above was taken from the file (and line numbers) cited in the undefined variable message and therefore directly relevant to the question. If the undefined variable was in the included file (ie. "name.php") then the included file would be cited in the "error" message and presumably the contents of that file would have been posted instead? Maybe?
Thank you all of you for taking the time to reply. I fear I have jumped in without thinking it all through and it may be a little more complicated than I originally thought.
Firstly, the problems only happen in my test server i.e. Wampserver and XAMPP, tried both. My actual website works fine online and I get no parsing errors - it validates to XHTML strict.
The problem is in my navigation code - I originally had just one vertical sidebar which I put in an include file, I then added a horizontal dropdown menu (as far as i know you can't do a PHP 'current page' for that like you can in a traditional menu) and there is some kind of conflict between the two. The vertical navigation works fine but when I click any page on my horizontal nav bar it shows the errors which appear to link to the code in my vertical navigation. Like I said it works fine online and the problem only came to light when I had to reinstall WAMPSERVER and it came up with errors.
Sorry, I don't really know how to explain the problem in an easy way and my knowledge of PHP is only basic so I will go away and have a think about what I am doing wrong. Thanks again for your time. I may yet need your help.
|Aside: Interesting use of a DL-list to display a list of links. (Why not an unordered list?) Note, however, that HTML5 requires at least one DT element (per DD group) to be valid. (HTML4 allowed the DT to be omitted.) |
When I learned it was at the time of XHTML as opposed to HTML4 and I was told it was better not to use unordered lists. Not sure if that was good advice as I have never see them used in that way.
|I originally had just one vertical sidebar which I put in an include file, I then added a horizontal dropdown menu (as far as i know you can't do a PHP 'current page' for that like you can in a traditional menu) and there is some kind of conflict between the two. |
Oh, this sounds so familiar. Make sure you're not passing the same parameter twice, and that the include/require file doesn't have parameters of its own. I've got a few pages that involve things like
where the page itself doesn't use the "page" parameter-- but its included navigation files do. The "page" parameter can be used as often as needed, but it has to be attached to the overall page, or there will be a conflict.
|...the problems only happen in my test server.... My actual website works fine online and I get no parsing errors |
Providing you have the same files on the test and live sites, then this is probably just a difference in the error_reporting level between the servers. The live site is probably just suppressing the error - it's still there. (Note that this is not a syntax/parse error, it is simply a runtime notice.)
Just to note, "undefined variable" messages (an E_NOTICE) doesn't necessarily mean the code is not working. PHP will default the value of the variable in such cases - which may be the correct thing to do, but it may not. It is an indication that there might be a problem. (Obviously it is always best practise to avoid such messages and they are always avoidable.) The default error_reporting level (E_ALL & ~E_NOTICE) suppresses these messages.
I have been looking over my code again and I am sorry that I didn't explain properly my issue and that is down to my inexperience with PHP.
The solution was really simple in the end but I could not see the wood for the trees.
I had a vertical menu which was correctly coded and the current page was highlighted using PHP. I added a horizontal dropdown menu which caused the undefined variable error message on my test server but was ok on the live site. When I had to reinstall my test setup I had obviously forgotten that I had suppressed the errors by editing in PHP ini. Anyway, the reason for the errors was oh so simple and any one of you guys would have seen straight away what was wrong if I had explained it better.
On the horizontal menu I did not define the variables on those pages - <?php $thisPage='page1';?> As I was not highlighting the current page on this menu I did not think I needed to define those pages. Another valuable lesson learned for me.
|As I was not highlighting the current page on this menu I did not think I needed to define those pages. |
Well, I wouldn't have thought so either :) But of course it isn't a question of whether you're physically treating the page differently; it's whether the code at some point looks for the variable.