Notice: Undefined variable and include files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Notice: Undefined variable and include files

    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

  • Steve

    #2
    Re: Notice: Undefined variable and include files

    [color=blue]
    > I'm new to PHP - moving over from ASP.[/color]
    [color=blue]
    > file, but get a "Notice: Undefined variable loginmsg" warning. I've[/color]

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

    ---
    Steve

    Comment

    • ZeldorBlat

      #3
      Re: Notice: Undefined variable and include files

      >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:
      [color=blue]
      >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?[/color]

      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.p hp');
      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.

      Comment

      • Chung Leong

        #4
        Re: Notice: Undefined variable and include files

        Mike wrote:[color=blue]
        > 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?[/color]

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

        Comment

        Working...