SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to reference a global var in another file

    Hi

    I have a php file (oops.php) that calls in "mma-config.php" which contains...

    global $sERROR_NO_CONN = "Error connecting to database. Check back later";
    global $sERROR_NO_ACCESS = "You are not authorized to access this area.";
    global $sERROR_ADD = "An error was encountered adding the record";
    global $sERROR_MODIFY = "An error was encountered modifying the record.";

    Inside oops.php, I call mma-config.php and want to reference the error message - example:

    if ($mysqli->connect_error)
    {
    $_SESSION['error'] = ERROR_NO_CONN;
    GoToOops();
    exit();
    }

    However when the oops php page is displayed, instead of the error message, it displays "ERROR_NO_CONN";
    I did check in the oops.php page that I called in the mma-config.php script
    Can't figure out what the problem is.

    Kath

  2. #2
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have any additional code that you can post? It's hard to determine scoping without seeing the full context.

    It looks like you might have the wrong idea about the global keyword from what you have posted though. Using the global keyword when declaring variables should result in a parse error (http://codepad.org/uygSzPAW)

    The correct usage of global is like so: http://codepad.org/nMtWxwlF

    global should be used withing functions to access variables that are declared in the global scope ($foo in both examples is declared in the global scope)

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not sure if you are getting mixed up with define, or whether you need the global keyword at all in this particular context.

    Otherwise just access the var as $sERROR_NO_CONN ie with an 's'.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •