Solved

Default php variable

Posted on 2011-02-21
3
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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.

737 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