doubushi0031 2014-07-02 19:10
浏览 18
已采纳

如果不立即调用php函数,我应该依赖什么?

I've tried regular functions and call_user_function, both which do not finish executing before they get to the next line after a function call. What do people use instead? Should I just include a separate php file for every "function" I want to run?

edit:

$IDNum = 0;


$restartButtonIDName;

$playButtonIDName;

$audioPlayerIDName;

$MP3AudioSourceIDName;
$OGGAudioSourceIDName;

$resultingTextIDName;

$currentTimeIDName;

$checkOffsetIDName;



call_user_func('setIDNames');



function setIDNames() {

    echo "called setIDNames <br />";

    call_user_func('incIDNums');

    $restartButtonIDName = "restartButton".$IDNum;

    $playButtonIDName = "playButton".$IDNum;

    $audioPlayerIDName = "audioPlayer".$IDNum;

    $MP3AudioSourceIDName = "MP3AudioSource".$IDNum;
    $OGGAudioSourceIDName = "OGGAudioSource".$IDNum;

    $resultingTextIDName = "resultingText".$IDNum;

    $currentTimeIDName = "currentTime".$IDNum;

    $checkOffsetIDName = "checkOffset".$IDNum;


}

function incIDNums(){

    echo "called setIDNums <br />";

    $IDNum += 1;

}

echo $restartButtonIDName." test<br />"; // echos "test" not the resulting name


?>
  • 写回答

2条回答 默认 最新

  • douzhizao0270 2014-07-02 19:21
    关注

    Variables accessed inside a function are not the same as variables outside the function, or in a different function, unless you declare the variables with a global statement inside the function.

    $IDNum = 0;
    incIDNums();
    echo $IDNum; // Will echo 1
    
    function incIDNums(){
        global $IDNum;
    
        echo "called setIDNums <br />";
        $IDNum += 1;
    
    }
    

    It's generally considered poor style to depend heavily on global variables, as it impairs the generality of the functions. The function should get its input via parameters, and sends the results as a value using return. If you need to return multiple results, you can package them into an array, or use reference parameters that are updated in place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备