Solved

Default php variable

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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