homepage Welcome to WebmasterWorld Guest from 107.20.37.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Syntax parse error
Undefined Variable
shug




msg:4682470
 9:38 pm on Jun 24, 2014 (gmt 0)

Hi, hope someone can help please?
I had to turn off error reporting in my PHP ini file to stop an error message. Here is a snip off the code. I get an undefinded variable message when errors are turned on.

<?php
include ('includes/name.php');
?>


<dl id="name">
<?php
if ($thisPage=='home')
{echo '<dd class="current">Home';}
else
{echo '<dd><a href="homepage.php" title="Home">Home</a>';}?></dd>
<?php
if ($thisPage=='somepage')
{echo '<dd class="current">somepage';}
else
{echo '<dd><a href="somepage.php" title="somepage">somepage</a>';}?></dd>
</dl>

Any help will be very much appreciated.

Regards
Hugh

 

lucy24




msg:4682498
 10:58 pm on Jun 24, 2014 (gmt 0)

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.

penders




msg:4682525
 12:16 am on Jun 25, 2014 (gmt 0)

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.)

lucy24




msg:4682548
 2:42 am on Jun 25, 2014 (gmt 0)

I was hoping the value of $thisPage comes from
include ('includes/name.php');
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.)

Edit:
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.

penders




msg:4682725
 1:26 pm on Jun 25, 2014 (gmt 0)

I was hoping the value of $thisPage comes from
include ('includes/name.php');


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?

shug




msg:4682805
 6:11 pm on Jun 25, 2014 (gmt 0)

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.
Hugh

lucy24




msg:4682835
 7:31 pm on Jun 25, 2014 (gmt 0)

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

/fun/panda.php?animal=robot&page=panda [L]

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.

penders




msg:4682847
 8:06 pm on Jun 25, 2014 (gmt 0)

...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.

shug




msg:4683794
 7:57 pm on Jun 29, 2014 (gmt 0)

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.

Thanks again.

lucy24




msg:4683817
 10:18 pm on Jun 29, 2014 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved