variable errors, PHP

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
phpsocko
Forum Newbie
Posts: 4
Joined: Mon Jul 16, 2007 9:15 am

variable errors, PHP

Post by phpsocko »

I'm using the following code in a PHP page:

Code: Select all

<?php
include_once('includes/functions.php');
require_once('includes/session.php');
?>
The page 'session.php' includes this code:

Code: Select all

if(!file_exists($filename)) {
	$rootSpec = "../";
}
require_once $rootSpec . 'error.php';
The page 'functions.php' includes this code:

Code: Select all

$rootSpec = rootSpec();

function rootSpec() {
	$filename = "foo.php";
	if(!file_exists($filename)) {
		$rootSpec = "../";
		return $rootSpec;
	}
}
The purpose of this code is to allow includes to be used in files in the current folder as well as files in other folders. The code works fine under PHP 5.2.1, but throws warnings under 5.2.3. Any ideas why this might be occurring?

Any help is appreciated.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

What are the warnings? Perhaps something about a variable not being declared?

I think that on your 5.2.1 box warnings/notices are off but they are turned on on your 5.2.3 box. They should always be on anyways.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This right here:

Code: Select all

<?php 
include_once('includes/functions.php'); 
require_once('includes/session.php'); 
?>
Literally parses as:

Code: Select all

<?php
$rootSpec = rootSpec(); 

function rootSpec() { 
   $filename = "foo.php"; 
   if(!file_exists($filename)) { 
      $rootSpec = "../"; 
      return $rootSpec; 
   } 
}

if(!file_exists($filename)) { 
   $rootSpec = "../"; 
} 
require_once $rootSpec . 'error.php';
?>
Which means that unless you have declared a value for $filename before the include of the sessions code, you are going to get undefined variable notices. The reason, I suspect, that you are not getting these in the other environment is because display errors is set to off or your error reporting level is less than E_ALL/E_STRICT.
phpsocko
Forum Newbie
Posts: 4
Joined: Mon Jul 16, 2007 9:15 am

Post by phpsocko »

Yes, warnings about variables not being declared. Is the syntax I'm using illegal? If so, what would you recommend changing?
phpsocko
Forum Newbie
Posts: 4
Joined: Mon Jul 16, 2007 9:15 am

Post by phpsocko »

Everah wrote: Which means that unless you have declared a value for $filename before the include of the sessions code, you are going to get undefined variable notices. The reason, I suspect, that you are not getting these in the other environment is because display errors is set to off or your error reporting level is less than E_ALL/E_STRICT.
I see what you're saying, but the warnings I'm seeing include the $rootSpec variable, which is defined before sessions.php is included. Evidently PHP thinks I'm doing something bad, which is why I'm seeing the warnings. What's the recommended way to handle this? Also, the code runs fine, and I'd like to leave it for now while I'm deciding how to correct it. How do I downgrade the error reporting level on the 2nd box?

Thanks for the help.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

phpsocko wrote:
Everah wrote: Which means that unless you have declared a value for $filename before the include of the sessions code, you are going to get undefined variable notices. The reason, I suspect, that you are not getting these in the other environment is because display errors is set to off or your error reporting level is less than E_ALL/E_STRICT.
I see what you're saying, but the warnings I'm seeing include the $rootSpec variable, which is defined before sessions.php is included. Evidently PHP thinks I'm doing something bad, which is why I'm seeing the warnings. What's the recommended way to handle this? Also, the code runs fine, and I'd like to leave it for now while I'm deciding how to correct it. How do I downgrade the error reporting level on the 2nd box?

Thanks for the help.
$rootSpec may be defined. Look at this code...

Code: Select all

<?php
$rootSpec = rootSpec(); 

function rootSpec() { 
   $filename = "foo.php"; 
   if(!file_exists($filename)) { 
      $rootSpec = "../"; 
      return $rootSpec; 
   } 
}
?>
If the file 'foo.php' does exists, then the function returns null. Add this this to just above the include to see what I mean:

Code: Select all

<?php
var_dump($rootSpec); echo '<br />';
require_once $rootSpec . 'error.php';
?>
And run the script again knowing that foo.php exists.
phpsocko
Forum Newbie
Posts: 4
Joined: Mon Jul 16, 2007 9:15 am

Post by phpsocko »

As far as I know, it is always defined, as every call to the variable, including the one in question, is preceded by this code:

Code: Select all

$filename = "foo.php";

if(!file_exists($filename)) { 
        $rootSpec = "../"; 
}

So is the PHP engine assuming that it may or may not be defined as well, and throwing warnings? If so (I ask again), what's the better way to handle this, and how do I change the error reporting levels? I looked up error reporting levels at php.net, and I can add error reporting definitions to a single page, but they only apply to that page. Do they need to be added to httpd.conf or some other file to make them global?

Thanks.
Post Reply