如何以另一种形式调用PHP中的函数?
我有一个名为 1.php
的页面,其中有一个函数。现在我想在PHP 2中调用2.php
中的函数。我写了func();
这个函数在1.php
中。
但它有这个错误:
Fatal error: Call to undefined function func() in C:\xampp\htdocs\me\2.php on line 2
我该怎么做?
I have a page with name 1.php
that has a function in it. Now I want to call the function in 2.php
in PHP 2. I wrote func();
this function is in 1.php
.
But it has this error:
Fatal error: Call to undefined function func() in C:\xampp\htdocs\me\2.php on line 2
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
require_once()
方法包含包含您需要的函数的脚本,如下所示:一旦您使用上述方法,PHP 基本上会将包含的脚本添加到父脚本中,这将允许您调用函数、方法和变量,就像它们位于父脚本本身中一样。
我会推荐
require_once()
而不是include()
、include_once()
或require()
,因为这方法将确保您正在查找的脚本存在,并且仅被调用一次。多次调用同一脚本(通常是偶然发生)可能会导致脚本中出现奇怪的问题。我希望这有帮助。
Include the script that contains the function you need using the
require_once()
method, like this:Once you use the above method, PHP basically tacks the included script into the parent script, which will allow you to call functions, methods, and variables, as if they were in the parent script itself.
I would recommend
require_once()
overinclude()
,include_once()
, orrequire()
, because this method will make sure that the script you are looking for exists, and is only called once. Calling the same script more than once (usually happening by accident) can cause strange problems in your script.I hope that helps.