Solved

Default php variable

Posted on 2011-02-21
3
229 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 82

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now