-3

How can I make a config.php file with all environment variables and import it successfully into other scripts?

config.php:

<?php
    $folder = 'SomePath';
?>

script1.php

<?php
    require_once('config.php');
    function doStuff(){
        global $folder;
        echo($folder);
    }

    doStuff();
?>

I get

PHP Notice:  Undefined variable: folder in script1.php

It's php version 5.4.41 and I can't do anything about it (no root)

El Dude
  • 5,328
  • 11
  • 54
  • 101
  • see edit, typo here, not in my script – El Dude May 17 '16 at 23:02
  • is config.php in the same folder with script1.php ? –  May 17 '16 at 23:03
  • yes, it' in the same folder – El Dude May 17 '16 at 23:03
  • 100% valid, something your not showing is going on –  May 17 '16 at 23:04
  • Is script1.php included by another script? – Cave Johnson May 17 '16 at 23:05
  • See new edit, works. Why do I have to put require into the function body? I didnt realize there was a difference. Thought of require as include in C... – El Dude May 17 '16 at 23:08
  • you are not calling the function, that's why it doesn't give you the error now. – Cave Johnson May 17 '16 at 23:09
  • im so sick of people not posting their real code, sigh –  May 17 '16 at 23:09
  • Do `echo getcwd();` in script1.php and see if it is the directory that config.php is in. – Cave Johnson May 17 '16 at 23:10
  • @Dagon, why post the full code that would reveal confidential info? – El Dude May 17 '16 at 23:10
  • Can somebody please answer why I had to put `require_once` into the function body instead to the top level? Why would `$folder` be unknown in the `doStuff` function? – El Dude May 17 '16 at 23:14
  • 1
    because variables are not global, functions have their own scope (http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) –  May 17 '16 at 23:15
  • There is no reason why you had to put `require_once` into the function body. From the code you posted, it should work. – Cave Johnson May 17 '16 at 23:18
  • how would I define global variables in config.php with PHP then? I looked at this example http://php.net/manual/en/language.variables.scope.php and why doesnt it apply? – El Dude May 17 '16 at 23:20
  • @Andrew yes there is, **varable scope**, if included out side the function the, the function would not see the variable inside the include –  May 17 '16 at 23:20
  • @Dagon yes but in the first version of his question, he didn't have a function. – Cave Johnson May 17 '16 at 23:21
  • @Andrew but thats not his code, he expects us to psychiclly guess what code he has –  May 17 '16 at 23:22
  • @Andrew That was my mistake. Sorry. I didnt realize the scope in a function would be clean. But it contradicts the example I posted above – El Dude May 17 '16 at 23:22
  • nothing in the manual page is wrong, i dont see why your not getting this –  May 17 '16 at 23:22
  • Oh ok then Dagon is correct, your functions can't see global variables. – Cave Johnson May 17 '16 at 23:23
  • @Dagon Well, guess that was my problem all along. If I move `require_once` out of `doStuff`, it fails again... – El Dude May 17 '16 at 23:23
  • yes, because the variable is outside the function scope. –  May 17 '16 at 23:24
  • But what's the difference between my code now and http://php.net/manual/en/language.variables.scope.php ? Why should it work for the tutorial example and fails for my particular example? – El Dude May 17 '16 at 23:25
  • are we looking at the same manual page : **This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope.** –  May 17 '16 at 23:27
  • That is not a tutorial. It is actually explaining what we are talking about here. – Cave Johnson May 17 '16 at 23:27
  • Ok, my bad. Sorry, RTFM in more datail, do not think C. – El Dude May 17 '16 at 23:28

1 Answers1

0

keeping it simple, if it was me:

//config.php:

<?php
    $folder = 'SomePath';
?>

//script1.php

<?php
 require_once('config.php');

    function doStuff($folder){//parse the variable in to the function

        echo($folder);
    }

    doStuff();
?>
  • Now that I understood the different scope philosophy I go with the global keyword. – El Dude May 17 '16 at 23:33
  • 1
    global is generally NOT recommended. to easy to overwrite other variables hard to maintain, better to keep to tight scoping. but if these are really globlas consider constants: http://php.net/manual/en/function.define.php http://php.net/manual/en/language.constants.php –  May 17 '16 at 23:34
  • Be aware of course that in this example, $folder is not being passed (note: passed, not parsed) in to the function. It is also a good idea to set a default value for your function parameters ( `function doStuff($folder = 'defaultFolderName'){`) or to check if they're `empty()` before attempting to use them. – Warren Sergent Jun 21 '16 at 03:47