且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 PHP 中的变量从一个文件传递到另一个文件

更新时间:2023-01-08 08:08:19

包含您希望变量可在其中访问的文件,就像这样

Include the file you would like the variable to be accessible within, like so

include('somefile.php')

并且在该文件的顶部您可能需要放置类似[取决于服务器配置]

and at the top of that file you might need put something like [depending on server configurations]

global $pubname

但在大多数情况下,您不需要这样做.

But in most cases you would not need to do this.

关于安全性,根据 $pubname 的设置方式,您的查询可能会或可能不会容易受到 sql 注入.

In regards to security, depending on how $pubname is set, your query may or may not be prone to sql injection.

注意:还有其他方法可以include() 文件,例如include_once()require()require_once(),来自 php.net:

Note: There are other means to include() files such as include_once(), require() and require_once(), from php.net:

以下文档也适用要求().这两个构造是除了他们如何处理失败.include() 产生一个警告而 require() 导致致命错误.换句话说,使用require() 如果你想要一个丢失的文件停止处理页面.include() 不会这样,无论如何,脚本将继续.一定要有合适的include_path 设置也是如此.是警告需要解析错误文件不会导致处理停止在 PHP 4.3.5 之前的 PHP 版本中.从这个版本开始,它确实如此.

The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. Be warned that parse error in required file doesn't cause processing halting in PHP versions prior to PHP 4.3.5. Since this version, it does.