简体   繁体   English

PHP的外部函数调用不起作用

[英]php External function call not working

I'm not sure if this question will require some code examples, but... 我不确定这个问题是否需要一些代码示例,但是...

Here's the situation: 情况如下:

I am working on a site that someone else has already done work on. 我正在其他人已经做过的网站上工作。 I am trying to call an external function located in a file, let's aay, 'myFunctions.php'. 我正在尝试调用文件中的外部函数,让我们说“ myFunctions.php”。 The previous person has function calls to this file in the main web page with no problem. 前一个人可以在主页上对该文件进行函数调用,没有问题。 I created a new short function and placed it in that same file. 我创建了一个新的短函数并将其放置在同一文件中。 The web page already has a 'require_once("myfunctions.php");' 该网页已经有一个'require_once(“ myfunctions.php”);' line. 线。 The web site is in Wordpress, though a highly customized site. 该网站是Wordpress中的一个高度定制的网站。 The new function works fine when placed in the page, however I would like to use it on more than one page. 新功能放置在页面中时效果很好,但是我想在多个页面上使用它。

How can I get the new function to work? 如何使新功能正常工作? Does it have something to do with Wordpress? 它与Wordpress有关吗?

It seems strange to me because the other functions that were already in the file work with no problem. 对我来说,这很奇怪,因为文件中已经存在的其他功能可以正常工作。

Thank you. 谢谢。


Next test: 下一个测试:

It seems the problem is in the calling somehow. 看来问题出在调用中。

I simplified the code in order to test this. 为了进行测试,我简化了代码。 Here is the code I have now: 这是我现在拥有的代码:

<?php
$user = getUserInfo($username);
$myTest = strTest();

echo $myTest;
?>

And in the external functions file funct.php: 并在外部函数文件funct.php中:

<?php

function strTest(){
   return "This test worked!";
}

?>

The above function "getUserInfo" is a older one that also resides in the funct.php and works just fine. 上面的函数“ getUserInfo”是一个较旧的函数,它也位于funct.php中,并且工作正常。

However, the new function "strTest", when called does not work and the page is blank from that line on. 但是,新函数“ strTest”在被调用时不起作用,并且该行之后的页面为空白。


Update! 更新!

Found it! 找到了!

Sorry to everyone who contributed, but it looks like it was just a stupid oversight. 对每个做出贡献的人都表示抱歉,但这似乎只是一个愚蠢的疏忽。 I had just realized that the other guy had the functions containing file in another folder. 我刚刚意识到,另一个人在另一个文件夹中具有包含文件的功能。 There was a copy of it in the folder I was working with, but the actual 'include' statement referred to the other folder. 我正在使用的文件夹中有一个副本,但实际的“ include”语句引用了另一个文件夹。

As with many problems I've had before, when the results seem very strange, the problem is usually syntax or address errors. 与我以前遇到的许多问题一样,当结果看起来很奇怪时,问题通常是语法或地址错误。

Thanks! 谢谢!

Move it to functions.php in stylesheet directory. 将其移动到样式表目录中的functions.php中。 All WP scripts will be able to call it. 所有WP脚本都可以调用它。

Most WP projects have at least one header file ; 大多数WP项目至少有一个头文件 ; if you call the function file from within any of these header files, it will be available to all the pages inheriting the header file. 如果您从这些头文件中的任何一个调用函数文件,则继承该头文件的所有页面都可以使用它。

Hope this helps. 希望这可以帮助。 Let me know if you need more details. 让我知道您是否需要更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM