如何在函数内部使用require_once [英] how to use require_once inside function

查看:424
本文介绍了如何在函数内部使用require_once的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想在函数内使用require_once但不能正常工作???实际上我的页面中有三个函数,我该怎么做....

请问有人吗??

这是我的代码:

 <?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>';
            }

    }

解决方案

实际上,从技术角度来看,您可以在函数内部使用require_once().但是,这很可能是一个坏主意,而不是您真正想做什么:

  • 在函数内包含代码的地方实际上就是在其中包含代码.这意味着将在函数范围内评估所有包含的代码. PHP将所有函数声明为全局函数,但是变量和纯代码序列在本地绑定,因此在当前执行的函数之外不可见.

  • 由于使用相对路径来加载包含的文件,因此只能从某个文件系统级别执行功能.这限制了代码的使用方式...

当包含的文件中包含一些本地配置时,这实际上可能是您想要的,但是您几乎肯定会迷失于此:

  • require_once()仅需要一次,这就是该函数的用途.这意味着:如果您多次输入函数getTierOne(),则对于随后的每次运行,require_once()都将不会包含任何代码,因为它已在第一次运行中包含.因此,是否包括您的配置取决于您!这是一个可怕的设计!

因此,要么全局包含您的配置,例如将其存储在某个变量中,然后您就可以在函数内部引用它,或者使用require()include()确保该配置确实包含在函数的每次执行中.

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>';
            }

    }

解决方案

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.

这篇关于如何在函数内部使用require_once的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆