Undefined Index Issue - Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JMichaels22
Forum Newbie
Posts: 5
Joined: Mon Feb 13, 2006 10:14 am
Location: Chicago Burbs, IL

Undefined Index Issue - Error

Post by JMichaels22 »

All,

I am trying to create the following short variable name:

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

When I try to do this I get the following error and im not too sure why:

Notice: Undefined index: DOCUMENT_ROOT in C:\Inetpub\wwwroot\processorder.php on line 68



All the tags are in place and correct and when I comment out that piece of code everything else will work. Any ideas on what I am missing, seems like I am missing something obvious, possibly PHP setup?

Thanks
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I've never encountered this problem before but I'm gathering from the manual that this variable isn't always available. Here is a tip in the suggestions on how to make it your self:

Code: Select all

if(!isset($_SERVER["DOCUMENT_ROOT")){
$_SERVER["DOCUMENT_ROOT"]=substr($_SERVER['SCRIPT_FILENAME'] , 0 , -strlen($_SERVER['PHP_SELF'])+1 );
 }
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

isset will still give you a undefined index error. instead of !isset() use empty()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isset() does not give undefined index errors.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

feyd wrote:isset() does not give undefined index errors.
well i'll be damned. i thought it gave undefined index errors and undeclared variable errors. well i guess you learn somtin new every day eh?
User avatar
JMichaels22
Forum Newbie
Posts: 5
Joined: Mon Feb 13, 2006 10:14 am
Location: Chicago Burbs, IL

How does this code work?

Post by JMichaels22 »

What exactly does this piece of code do? I understand that isset is testing to see if the variable is empty, then if it is it sets the variable. What I dont get is the last part of this code, what is it doing?

Thanks for the replies and for being patient =)

-Jassen
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

attempts to extract a substring from another variable.
User avatar
JMichaels22
Forum Newbie
Posts: 5
Joined: Mon Feb 13, 2006 10:14 am
Location: Chicago Burbs, IL

Post by JMichaels22 »

Thanks that did do the trick!

Im still confused as to what this if statement does, but i'll get it sooner or later. =)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The if statement is checking to see if there is a value set for $_SERVER["DOCUMENT_ROOT"]. If there isn't one, then it makes one for you based on values from a few other $_SERVER vars.

Code: Select all

<?php
if(!isset($_SERVER["DOCUMENT_ROOT"])) {
    // If $_SERVER["DOCUMENT_ROOT"] does not have a set value...
    // make one using this scripts file name
    $_SERVER["DOCUMENT_ROOT"]=substr($_SERVER['SCRIPT_FILENAME'] , 0 , -strlen($_SERVER['PHP_SELF'])+1 );
} 
?>
Post Reply