Reputation: 5234
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
?>
Upvotes: 0
Views: 43
Reputation: 781300
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.
Upvotes: 3
Reputation: 2704
Probably the problem you have is due to the scope of variables ,please note the lifetime of variables inside a function is only until function is active to make changes effect to global variables which are outside of function you need to make them global inside function like:
function setIDNames() {
global $restartButtonIDName ,$playButtonIDName ,$audioPlayerIDName ...;//All variables you wanna use saperated by comma.
//Your code
}
Upvotes: 1