0

How to make variables visible when including some file. For example:

code.php:

<?php
   global $var; $var = "green";
?>

index.php:

<?php

include("code.php");

Function index(){
   echo "The apple is $var";
}

?>

Please note that in code.php there are a lot of global variables (~150 variables) and all variables are used in many different functions inside the index.php.

  • 1
    This isn't an issue with including, but an issue with variable scope. You don't need to define it as `global` in `code.php`. When you include a file, you inherit all those variables. You need to pass it as an argument in the function, `function index($var) {}` and then call that function `index($var);` – Qirel Jun 02 '16 at 13:55
  • For further reading, take a look at this: [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Qirel Jun 02 '16 at 13:57
  • @Qirel thank you for your reply – Werner Heisenberg Jun 02 '16 at 13:59

2 Answers2

1

This is an issue to do with variable scope, plus you do not need to be defining $var as a global.

When you include a file, you can imagine in your head that it is just copy-pasting the contents of the other file into the current file.

E.g.

code.php

$includedName = 'Tom';

index.php

include 'code.php';

function sayHello($name)
{
    echo 'Hello ' . $name;
}

sayHello($includedName); // Hello Tom
Tom Wright
  • 2,841
  • 4
  • 22
  • 30
  • Is that 'scope' is the only way to solve my problem? Can we create any classes to solve it without scope? Because the whole code has been already wrote and I think there is no need to make scope, beacuse I'm trying to localise a website, so I need to include language strings from language files. – Werner Heisenberg Jun 02 '16 at 14:02
  • It's not something that *solves* your problem, it's the way PHP works, and it is the root if your issue. Without seeing the full code, I can't do much but guess, but generally speaking, the solution above is the best way to go. You pass the variable as an argument in the functions, and use it like that. – Qirel Jun 02 '16 at 14:04
  • If you show a bit more specific part of your code, we might be able to assist a bit more and give better advice. – Qirel Jun 02 '16 at 14:10
0

You've mentioned you are working with legacy code, so it may be worthwhile to preserve the use of globals for the sake of consistency - though using globals is generally considered to be very bad practice, I'd generally consider inconsistently using globals to be worse.

To break function scope and pull in variables from the global scope you must invoke the global keyword from within the function:

<?php

$var = "green";

Function index(){
   global $var;
   echo "The apple is $var";
}

?>

This answer sums up why global variables are considered to be bad practice:

There's no indication that this function has any side effects, yet it does. This very easily becomes a tangled mess as some functions keep modifying and requiring some global state. You want functions to be stateless, acting only on their inputs and returning defined output, however many times you call them.

However, in this specific example, you are not modifying $var's state - only reading it. So the issues are minimal.

The problems with global state can be read about in more depth on Programmers.SE.

Community
  • 1
  • 1
HPierce
  • 7,249
  • 7
  • 33
  • 49