如何以另一种形式在PHP中调用函数? [英] How to call a function in PHP in another form?

查看:149
本文介绍了如何以另一种形式在PHP中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 1.php 的页面,它有一个函数。现在我想在PHP 2中调用 2.php 中的函数。我写了 func(); 这个函数是在 1.php

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?

推荐答案

使用 require_once()方法包含包含所需函数的脚本,如下所示:

Include the script that contains the function you need using the require_once() method, like this:

require_once("../path/to/script.php");

一旦您使用上述方法,PHP基本上会将包含的脚本粘贴到父脚本中,你可以调用函数,方法和变量,就好像它们在父脚本本身一样。

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.

我会推荐 require_once() over include() include_once() require(),因为此方法将确保您正在查找的脚本存在,并且只被调用一次。多次调用相同的脚本(通常偶然发生)可能会在您的脚本中导致奇怪的问题。

I would recommend require_once() over include(), include_once(), or require(), 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.

我希望这有帮助。

这篇关于如何以另一种形式在PHP中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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