Include file gives undefined variable warning

General discussion on Zend Studio

Include file gives undefined variable warning

Postby gryphonws on Sat Aug 15, 2009 1:42 pm

This is probably going to be a very simple question, but I have searched for about an hour and still haven't found the solution.

I installed Studio 7 and created a new PHP project with 2 PHP files:

- test.php
- include.php

include.php:
Code: Select all
$test = 'test';


test.php:
Code: Select all
include("include.php");
echo $test;


Zend studio shows a warning in test.php: Undefined variable '$test'. Why? Both files are in the same folder, so test.php should be able to include include.php..

Thanks!
John
gryphonws
 
Posts: 8
Joined: Sat Aug 15, 2009 1:40 pm

Re: Include file gives undefined variable warning

Postby albertsj1 on Fri Aug 21, 2009 3:06 pm

I have been wondering this myself. I searched the forums for 'undefined variable', but only see a bunch of posts about a problem with undefined variables in control structures.
albertsj1
 
Posts: 5
Joined: Fri Aug 21, 2009 3:04 pm

Re: Include file gives undefined variable warning

Postby seopaul on Fri Aug 21, 2009 3:44 pm

the error is an error from php itself rather than ZS, it's because your meant to check the existence of a variable before using it, to stop the error you can use the isset() function.

it only checks that the variable exists so if you wanted to check it has a value use !empty()

Code: Select all
if(isset($test)){
echo $test;
}

// or to check the vaiable has data and its not just a variable with a value of null
if(!empty($test)){
echo $test;
}


hope that helps
seopaul
 
Posts: 41
Joined: Wed Jun 10, 2009 1:37 pm

Re: Include file gives undefined variable warning

Postby albertsj1 on Fri Aug 21, 2009 4:16 pm

seopaul wrote:the error is an error from php itself rather than ZS, it's because your meant to check the existence of a variable before using it, to stop the error you can use the isset() function.

it only checks that the variable exists so if you wanted to check it has a value use !empty()

Code: Select all
if(isset($test)){
echo $test;
}

// or to check the vaiable has data and its not just a variable with a value of null
if(!empty($test)){
echo $test;
}


hope that helps


Thanks for the quick reply. Sorry. I guess I should have clarified where the problem exists. The message I am referring to is in the studio editor, not a php message. The various variables that are previously defined in an include file and are referenced in the open file have a squiggly line under them and a tooltip pops up that says "undefined variable ...", even though that variable is defined in an include file.
This pretty much makes the built-in editor error checking useless for me.

Thanks
John
albertsj1
 
Posts: 5
Joined: Fri Aug 21, 2009 3:04 pm

Re: Include file gives undefined variable warning

Postby totalsupport on Fri Aug 21, 2009 4:19 pm

I believe the topic-starter meant something else. When you run this code, there will be no warning. It's the code-analyzer that goes nuts if you do something like this (it'll show up in the 'Problems'-tab and it will have the yellow wigly-line beneath it, etc).

Replacing test.php with:
Code: Select all
include("include.php");
if(isset($test)){
  echo $test;
}

will still give a "used-before-defined" error (twice, for $test).

The problem here is that the code-analyzer doesn't actually 'run' your script, it just looks at the structure. You might think this is stupid, but actually the usage of php is what's at fault here. It is a (extremely) bad coding practice to have variables in the global scope like that and using them in multiple files. Use functions/classes/etc...

If you don't want to it the right way ;), but want the warning to bugger off, do it like this:
test.php:
Code: Select all
$test=''; // Initialize it as an empty string.
include("include.php");
echo $test;

The thing here is that test.php DOES NOT KNOW what happens to $test in include.php! Neither should YOU as a programmer or the code-analyzer.
totalsupport
 
Posts: 123
Joined: Thu Aug 20, 2009 2:38 pm

Re: Include file gives undefined variable warning

Postby gryphonws on Fri Aug 21, 2009 4:35 pm

The problem here is that the code-analyzer doesn't actually 'run' your script, it just looks at the structure. You might think this is xxxxxx, but actually the usage of php is what's at fault here. It is a (extremely) bad coding practice to have variables in the global scope like that and using them in multiple files. Use functions/classes/etc...


Why would it be (extremely) bad coding practice to include a simple configuration file for example with some predefined variables so you can easily use them in various php files? Sure, it might not be OOP, but last time I checked OOP wasn't the only programming method for PHP.

There are tons of non-OOP scripts which use includes this way and whenever I import those scripts in a new project, I get a LOT of warnings in Zend Studio. I don't want to "fix" those scripts by initializing the variables from those includes in the php scripts that use them, simply because it takes a LOT of time and it's useless in my opinion. Ideally, I would like Zend Studio to actually check the included files so it only shows "undefined variable" when it actually should. If that can't be done (which is what I understand from your answer), I would like Zend Studio to not mention the "undefined variable" warnings at all. Would any of those options be possible?

Thanks!
gryphonws
 
Posts: 8
Joined: Sat Aug 15, 2009 1:40 pm

Re: Include file gives undefined variable warning

Postby albertsj1 on Fri Aug 21, 2009 4:45 pm

I don't know that I agree with the extremely bad coding practice comment, but in any case, I'm working with an older project that I started helping out with, so it's not really feasible to change all of the code at this point. Defining the variable before the include file as you suggested does work; however, I would have to do that for every file that contains any reference to the variables in the 1 common file that is included in all of these pages.
Do you know if there is any way to define for a project values for various php variables that will be used as defaults for each page in a project?
For instance, if I defined in an include file in my project something like '$libDir="/opt/includes";', is there a way to define $libDir=/opt/includes for the entire project for the code-analyser?
albertsj1
 
Posts: 5
Joined: Fri Aug 21, 2009 3:04 pm

Re: Include file gives undefined variable warning

Postby totalsupport on Fri Aug 21, 2009 4:48 pm

Configuration variables are usually defined as constants. But this is an entirely different discussion. Just saying that, in the long run, you'll be happy when you've tried to avoid these kind of constructions... ;) I know it's not viable to start refactoring all old projects you run into. This is just something to keep in mind.

Anyway, fortunately you can completly customize the warnings that the analyzer gives you.

Go to Preferences -> PHP -> Code Analyzer.
You'll find your warning under the 'Security' section (just saying, just saying,... ;)): "Use of global variable before definition".
Change the value on the right from "warning" to "ignore".
Last edited by totalsupport on Fri Aug 21, 2009 4:53 pm, edited 2 times in total.
totalsupport
 
Posts: 123
Joined: Thu Aug 20, 2009 2:38 pm

Re: Include file gives undefined variable warning

Postby albertsj1 on Fri Aug 21, 2009 4:49 pm

@gryphonws,
Ah, so I guess we were talking about the same issue. :) Yes, I'm looking for a solution to the same problem. Because of this include file issue, I have over 7000 warning messages in my 'problems' tab for undefined variables which are actually defined in the include file.
albertsj1
 
Posts: 5
Joined: Fri Aug 21, 2009 3:04 pm

Re: Include file gives undefined variable warning

Postby seopaul on Fri Aug 21, 2009 4:52 pm

gryphonws wrote:Why would it be (extremely) bad coding practice to include a simple configuration file for example with some predefined variables so you can easily use them in various php files? Sure, it might not be OOP, but last time I checked OOP wasn't the only programming method for PHP....!


correct, but its good practice to check the existence of variables before using them regardless of design pattern.


Thanks for pointing that out bill, i moved away from using globals awhile ago so abit rusty debuging those errors, rather than starting the variable prior to the include simply use global $test before you want to use the variable,


Code: Select all
include 'test.php';
global $test;
if(isset($test)){
echo $test;
}



albertsj1 wrote:@gryphonws,
Ah, so I guess we were talking about the same issue. :) Yes, I'm looking for a solution to the same problem. Because of this include file issue, I have over 7000 warning messages in my 'problems' tab for undefined variables which are actually defined in the include file.


using the above method will stop warnings by bringing the global variable into local scope.
Last edited by seopaul on Fri Aug 21, 2009 4:55 pm, edited 1 time in total.
seopaul
 
Posts: 41
Joined: Wed Jun 10, 2009 1:37 pm

Next

Return to Zend Studio

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests