Page 1 of 1

variable errors, PHP

Posted: Mon Jul 16, 2007 11:40 am
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.

Posted: Mon Jul 16, 2007 11:57 am
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.

Posted: Mon Jul 16, 2007 1:17 pm
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.

Posted: Mon Jul 16, 2007 1:17 pm
by phpsocko
Yes, warnings about variables not being declared. Is the syntax I'm using illegal? If so, what would you recommend changing?

Posted: Mon Jul 16, 2007 1:38 pm
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.

Posted: Mon Jul 16, 2007 2:19 pm
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.

Posted: Mon Jul 16, 2007 2:43 pm
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.