By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Notice: Undefined variable and include files

P: n/a
I'm new to PHP - moving over from ASP.

I have a number of include files, the first of which sets the value of
a variable $loginmsg. I use that variable in a subsequent include
file, but get a "Notice: Undefined variable loginmsg" warning. I've
had a good look at previous posts concerning this warning, and see that
isset() is recommended to prevent this kind of warning. Is this
totally necessary?

In ASP, and include file becomes part of the total script for the page
that calls the include. Is this different in PHP? Is each include
treated as a separate script?

TIA

Mike

Oct 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I'm new to PHP - moving over from ASP. file, but get a "Notice: Undefined variable loginmsg" warning. I've


PHP variable names are case-sensitive. See
<http://www.php.net/manual/en/language.variables.php>.

---
Steve

Oct 31 '05 #2

P: n/a
>I've had a good look at previous posts concerning this warning, and see >that isset() is recommended to prevent this kind of warning. Is this totally >necessary?

isset() prevents the warning, but there's a bigger issue here. Not so
much caring whether the variable was set, but where it might have been
set. Suppose you include file inc1.php and he sets some variable.
Then later you include inc2.php. You can't really be sure that by the
time you get to inc2 the value of the variable is the same as it was
when you left inc1. Perhaps it shouldn't be if some other processing
occured, but this point leads into the next question:
In ASP, and include file becomes part of the total script for the page
that calls the include. Is this different in PHP? Is each include
treated as a separate script?


When you use include() or require(), you've effectively picked up any
code in that file and dropped it in where you call include(). That
means that the code in the included file lives in the same scope where
include() was called. For instance, suppose you had an include file
called inc1.php that contained the following:

<?
echo $foo;
?>

In your main script, you have something like this:

$foo = 5;

function bar($x) {
$foo = $x+1; //this foo is local to the function bar
include('inc1.php');
return $foo;
}

bar(1); //call the function

This will output 2. However, if you move the include() outside the
function declaration the script will output 5.

Nov 1 '05 #3

P: n/a
Mike wrote:
I'm new to PHP - moving over from ASP.

I have a number of include files, the first of which sets the value of
a variable $loginmsg. I use that variable in a subsequent include
file, but get a "Notice: Undefined variable loginmsg" warning. I've
had a good look at previous posts concerning this warning, and see that
isset() is recommended to prevent this kind of warning. Is this
totally necessary?


One thing to remember in PHP is that global variables are not
automatically availabe within functions.

Nov 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.