[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Default php variable

Posted on 2011-02-21
3
Medium Priority
?
241 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 84

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 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