Solved

Default php variable

Posted on 2011-02-21
3
236 Views
Last Modified: 2012-06-21
I'm calling an include at the very top of my code:

includes/page_top.php

i've got this line of code in includes/page_top.php:

<?php if ($crumb2=="wirtzer") echo "Active";?>

now, in the code of the page that is calling includes/page_top.php, i'm defining the crumb2 variable: i.e.:

<?php $crumb2="wirtzer";?>

my issue is that i'm getting server errors (not on the php page but in the log files) like this one:


[Sat Feb 05 10:03:06 2011] [client 76.168.200.119] PHP Notice:  Undefined variable: crumb2 in /home/derm/includes/submenu-forms.php on line 3

i'm guessing this is because includes/page_top.php loads before the rest of the code containing the actual crumb2 variable definition. can i define a default crumb2 variable in includes/page_top.php? so i don't keep getting these errors?




0
Comment
Question by:phillystyle123
3 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34948060
The definition needs to come before the 'include' or at least before the 'if'.  You can define and re-define it wherever you want.  It just has to be defined when you try to use it like in the 'if' statement.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34951122
This message is a Notice, not an Error:

[Sat Feb 05 10:03:06 2011] [client 76.168.200.119] PHP Notice:  Undefined variable: crumb2 in /home/derm/includes/submenu-forms.php on line 3

In the default installation of PHP, Notices are suppressed.  That means that you can use undefined variables as if they were NULL.  However you have error_reporting() set to display the Notices.  

You can handle this two ways.  You can set error_reporting(E_ALL ^ E_NOTICE) or you can test the variables to see if they are set before you try to use them in other expressions.  The second practice is better from the computer science standpoint.

A third way is to deliberately define the variables with a default value, then overwrite the default value with the data from whatever source you choose.
<?php // SHOW HOW TO HANDLE POTENTIALLY UNDEFINED VARS
if (isset($crumb2))
{
    if ($crumb2 == "wirtzer") 
    {
        echo "Active";
    }
}
?>

Open in new window

0
 

Author Closing Comment

by:phillystyle123
ID: 34975738
thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question