简体   繁体   中英

Global access of PHP vars

I looked into past answers and was led to believe PHP variables assigned in a header file are global. I must be doing something wrong then. Here's a much-simplified version of my code:

This is my myGlobals.php file:

<?php

  $NAMELABEL = "Name";

?>

This is file index.php that 'requires' myGlobals.php then calls a function in a 3rd php file

<?php

   require_once 'myGlobals.php';
   require_once 'DisplayRecords.php'; 

   displayRecords(); 
?>

And here is DisplayRecords.php

<?php

  function displayRecords()
 {
    echo '<table border="1">';
    echo '<tr>';
    echo '<th>' . $NAMELABEL . '</th>';
    echo '</tr>';
    echo "</table>";
  }
?>

This is HIGHLY simplified, only the code causing the error.

The error msg. I get when I run it is:

Notice: Undefined variable: NAMELABEL in C:\xampp\htdocs\fbreverse\DisplayRecords.php on line 29

Now, before I changed to the global variable $NAMELABEL -- I used this and it worked fine:

   echo '<th>Name</th>';

by 'fine' I mean that the table was created and it had a header called Name as on of the columns.

NOTE: using single quotes around the html tags above is allowed, and needed if you use a double-quoted string in the tag, as I did in echo '<table border="1">' -- so I just stayed consisted and stuck with the single quote. Changing the above example to

 echo "<th>" . $NAMELABEL . "</th>";

made zero difference, same error message, Notice: Undefined variable: NAMELABEL in C:\xampp\ht........blah blah

Why isn't $NAMELABEL visible in the function displayRecords() inside DisplayRecords.php?

You need to add at the top of your function...

global $NAMELABEL;

This will make $NAMELABEL available inside of your function.

Globals however should be minimised. Could you re-factor your code?

I looked into past answers and was led to believe PHP variables assigned in a header file are global.

Not within functions, they're not, unless you specifically tell the function to treat it as global.

function myFunction() {
  global $myVariable;
}

See http://php.net/manual/en/language.variables.scope.php for more details on variable scope in PHP.

As a side note, the way you're using this appears better suited to constants , which won't need to be declared as global - they are always global.

Because of variables scope . If you need to pass something into the function - use function arguments .

And no - global in other answers is never a good idea.

Because a variable is declared in the global scope doesn't mean it is accessible everywhere. Variables are not handled the same way as Javascript does. However, using classes and OO paradigms, the variables declared inside classes behave the same as in other languages (ie. Java, C#, etc.). This is a vestige of earlier versions of PHP.

So, if $NAMELABEL is a constant (non changing variable), a solution could be to use define() instead.

define('NAMELABEL', 'Name');

and use it anywhere in your code:

function displayRecords()
{
   echo '<table border="1">';
   echo '<tr>';
   echo '<th>' . NAMELABEL . '</th>';
   echo '</tr>';
   echo "</table>";
}

But yet a better idea would be to create a class and store the values therein. For example:

class Labels {
    static public $NAME = "Name";
}

and use it like this

function displayRecords()
{
   echo '<table border="1">';
   echo '<tr>';
   echo '<th>' . Labels::$NAME . '</th>';
   echo '</tr>';
   echo "</table>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM