无法在exit()内调用函数

时间:2012-07-11 16:42:37

标签: php function undefined

commons.php

中定义了一个函数
desktop.php -> include commons.php
    |
    |
   \|/
    include MODULES.'mod.php'

我可以在任何我想要的地方调用自定义函数,但不能在if内部的出口内调用。调用函数的代码不会出现:

Mod.php:

....
$error = mysql_error();

if($_ADM['id_user']==1) {
    if(!empty($error)) {
        $debug = array(
                    'message' => "SQL Error in infography_edit module.",
                    'line'    => '79',
                    'error'   => $error,
                    'SQL'     => $SQL
                );
        //exit(myPrint($debug)); //Calling here myPrint does not work
          exit(print_r($debug)); //This works
     }
 }

 $test = array('alex');
 exit(myPrint($debug)); //Calling here myPrint works

....

// The output error: Call to undefined function myPrint()

我只是无法理解为什么上面代码之外的任何其他地方都可以工作,但是如果没有在其中再次定义

更新

这样做,似乎也不起作用:

myPrint($debug);
exit();
// The output error: Call to undefined function myPrint()

UPDATE2

desktop.php主文件:

  1. 需要(LIBS.'commons.php');
  2. 常见的html
  3. 包含模块
  4. 包含桌面代码的键盘:http://codepad.org/hn8QlHQ9

5 个答案:

答案 0 :(得分:1)

那样的解决方法
function my_exit($msg){
   echo $msg;
   exit(1);  //Return code of the script, useful for cli scripts
}

//...
my_exit("Show message");
//...

答案 1 :(得分:0)

假设您的条件标准得到满足,您的代码应该有效。

  

调用未定义的函数myPrint()

上述错误表示未定义函数myPrint()。您可以使用function_exists()函数检查特定功能是否退出。如果它不存在,那么您可以在当前上下文中重新定义它。

<?php
if (function_exists('myPrint')) {
    echo "myPrint() exists.\n";
} else {
    echo "myPrint() doesn't exist.\n";
}
?> 

答案 2 :(得分:0)

exit()是一种语言结构,只能接受intstring类型的标量值。如果您需要在退出脚本之前打印出任何内容,我建议您在退出之前执行此操作。

....
$error = mysql_error();

if($_ADM['id_user']==1)
    if(!empty($error))
        $debug = array(
                    'message' => "SQL Error in infography_edit module.",
                    'line'    => '79',
                    'error'   => $error,
                    'SQL'     => $SQL
                );
        myPrint($debug);
        exit();
....

答案 3 :(得分:0)

您是否有机会使用命名空间?你确定你的功能是定义的吗?如果包含它,您确定已包含它吗?

P.S。 RobB很抱歉让你知道,但确实有效。

示例:

$v = 1234;
function xyz($v){
    echo $v . 23;
}
exit( xyz($v) );

答案 4 :(得分:0)

好的,首先我必须澄清我正在处理的代码,不是我的代码,我只是被雇用来做一些改进,并且不得不与主要开发人员交谈以解释这种混乱。

我一直缺少的是每个请求(每一个与页面刷新有关的请求)都经过一个名为do.php的辅助文件,最终加载了我所在的模块。

视觉解释:

Currently at          Action                Pre-Processor        Processor 
-------------    =>   ---------------   =>  -------------    =>  ---------
add_users.php         post from form        do.php               add_user.php

现在,您知道,当前模块位置(http://site.com/module?=add_users)保存在会话中,do.php有几行代码来检查该会话,如果一切正常则包含该模块。我知道这是一个巨大的混乱,但我对此无能为力。

所以,最后我必须转到do.php并在文件开头附近添加以下行:require_once('libs/commons.php');

我的推荐

每当您看到某个函数未加载,但您知道它在某些情况下完全加载时,请查找可能会干扰上述do.php的任何其他文件,并尝试包含/要求包含该文件功能。

我知道那些令人头疼的东西,但它就是这样,而且大部分时间不是我们的错,而是平台已经写好的方式。

祝所有人好运