0

I have a 3 php files, lets say [a,b,c].php

a.php does

$constants=parse_ini_file(ROOT_DIRECTORY."constants.ini", true);

b.php uses constants from that file via

function func(){
 var_dump($constants["xyz"]["abc"]);
}

and c.php includes them both

include "a.php";
include "b.php";

func();

Now when i visit /c.php I expect that it should be able to display the constants, but instead I get Undefined variable: constants in b.php

Is this expected to not work? Or am I doing something wrong?

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • FWTW: looks like it should work to me. –  Jul 05 '14 at 03:14
  • You will have to show `a.php` and `b.php`, and check if variable declaration and access are really in the same scope. – mario Jul 05 '14 at 03:17
  • I tried peppering the files with `var_dump($constants["xyz"]["abc"]);` And what I am seeing is, it works in `c.php` and top level `b.php` (when included), but not in function level in `b.php` when the function is called. – Karthik T Jul 05 '14 at 03:25
  • php 5.4.6 if it matters – Karthik T Jul 05 '14 at 03:27
  • 1
    No, that will not work. PHP doesn't have true global variables; they're all scoped. And `$constants` isn't within your func() implicitly. – mario Jul 05 '14 at 03:27
  • @mario ah.. So top level vars cannot be accessed within functions? odd. Can you post that as an answer with a reference or 2? So here the fix is to pass it in as params? Whats the best way to handle this sort of constants in ini situation? – Karthik T Jul 05 '14 at 03:29
  • @mario thanks for the pointer, I am using `global` to get around this issue – Karthik T Jul 05 '14 at 03:34

0 Answers0