5

Hello i want to use require_once inside a function but not working??? actually i have three functions in my page how can i do that.... its working out side but not inside functions ???

Any one please ????

here is my code:

 <?php
    //**************************************
    //     Page load dropdown results     //
    //**************************************
    function getTierOne()
    {
        require_once('../config.php');
        $provincequery="SELECT provinces.ProvinceID, provinces.ProvinceName FROM provinces WHERE ProvinceID > 0";
$result=$coon->query($provincequery); // mysqli neeeds connection while running the query
          while($province = mysql_fetch_array($result)) 
            {
               echo '<option value="'.$province['ProvinceID'].'">'.$province['ProvinceName'].'</option>';
            }

    }
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
user2279037
  • 77
  • 2
  • 3
  • 9

2 Answers2

4

Actually, form a technical point of view, you can use require_once() inside a function. However it is most likely a bad idea and not what you really want to do:

  • including code inside a function literally includes the code there. This means that all included code is evaluated inside the scope of the function. PHP declares all functions global, but variables and plain code sequences are bound locally, so not visible outside the function executed right now.

  • since you use a relative path to load the included file you are limited to execute your function from within a certain file system level. This limits how your code can be used...

Whilst this might actually be what you want when the included file holds some local configuration you will almost certainly stumble over this:

  • require_once() requires only once, this is what the function is there for. This means: if you enter your function getTierOne() more than a single time then for every subsequent run the require_once() simply won't include any code, since it already has in the first run. So wether you get your configuration included or not depends! That is a horrible design!

So either include your configuration globally and for example store it inside some variable which you can then refer to inside your function or you use require() or include() to make sure the configuration really is included in every execution of the function.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • sorry how to include my files inside a globall configuration – user2279037 May 02 '13 at 06:33
  • Well, by simply using `require_once()` at the beginning of your script, outside function scope if it holds static content. Note however that if you simply include your configuration then any syntax error in there will completely break your code. For dynamic content is is oftern safer to interpret the contents of such a file, for example by using the function `parse_ini_file()`... – arkascha May 02 '13 at 06:36
  • i have used the require_once() at the begining of my code how to use the parse_ini_file() function ??? sorry for my stupid question i am new in php – user2279037 May 02 '13 at 06:48
  • Well, you did take a look at the php documentation? You cannot program php without that: http://php.net/manual/function.parse-ini-file.php – arkascha May 02 '13 at 06:51
  • what about storing those values in static variables inside that function? Would that not be a useful design to spare resources by reading config.php only once and only when needed, by calling a function? – papo Feb 12 '19 at 11:53
  • @papo I would suggest you follow the established and proven programming pattern instead and create a service for that. A service that reads, caches and makes available the content of such files. You can then use that service wherever you like and you can either design it as a singleton or instantiate it via a service factory with also makes unit testing such a feature easy. – arkascha Feb 12 '19 at 16:48
  • Sure he can. But maybe he doesn't want to add a robust class to his php for reading one php config. And as this question was: 'how to use require_once inside function', I just thought it might be nice to use another crazy pattern - the static variable in a function to join in and do what OP asked. It's in php docs, so it should be supported, but I actually never tried that, that's why I asked and not stated that. – papo Feb 13 '19 at 11:12
  • @papo Sure, endless options, a huge universe to explore and make all sorts of mistakes others have already done thousands of times. – arkascha Feb 13 '19 at 19:47
  • I understand your view, but not your point. Please explain. Provide an argument. Why it shall be wrong? I just tested it and it does work as expected and documented. For either reason OP might have, hiding the value from global scope, or trying to minimize file read, it would seem to be an optimal way. Two lines of code, used in proper way, unambiguous. I really don't see an evil here. You might be right and have a perfect argument. That's why I was just asking. But if not, just fix your answer and let's be done with it :) – papo Feb 14 '19 at 02:06
  • Oh sorry, no, I did _not_ want to express that your approach is _wrong_. In my last comment the "mistakes others have already done" referred to the general aspect of a risk you take if you derive from well known and proven patterns for a task. I did _not_ want to say that your approach is wrong, I thought that was clear from my opening with "endless options". If something is an option, then it is valid and by that not _wrong_. – arkascha Feb 14 '19 at 11:33
0

I would first not recommend using require_once or require in a function, while yes it is possible to use this I personally have ran into issues and solved the issue by using include_once this seems to fix the error.

Also another thing you may want to look into, is finding the absolute pth for e.g. /home/username/public_html/system/core.php might be where the file is.

so you would use something like this

include_once("/home/username/public_html/system/core.php"); 

This should solve any issues your having.

on a side note, you should never put your config file or any file that is required at any state of the script inside a function it should always be called whether you use it or not. By what I can tell, it is the mysql connect file so I would always have that up the top before anything.

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • both: require_once("C:\xampp\htdocs\spdealers\Admin Panel\config.php"); and require_once(config.php); works out side of FUN not inside – user2279037 May 02 '13 at 06:08