Passing Tropo variables to PHP Functions

JustinDupree -

PHP's variable scoping rules mean that you can't simply use a globally-defined variable inside a function. It must be explicitly passed into the function. For example, if you wanted to use $currentCall->callerID into a function, you would need to do something like this:

<?php

_log("---------------- caller: " . $currentCall->callerID . " ---"); 
_log("---------------- called: " . $currentCall->calledID . " ---"); 

say(" . start"); 

test_func($currentCall); 

function test_func($currentCall) { 
    _log("-------- caller in function: " . $currentCall->callerID . " ---"); 
    _log("-------- called in function: " . $currentCall->calledID . " ---"); 

    say(" . Stop"); 
} 
 
?>

An alternative is to utilize the global keyword like so:

<?php
call("1407409XXXX", array(
       "callerID" => "604120XXXX", 
       "timeout"      => 59, 
       "onAnswer" => "CALLanswer" 
   ) 
);
function CALLanswer($event) {
   global $currentCall;
   say("Tag, you're it!");
   _log("********* caller ID is " . $currentCall->callerID . " ***********");
}
?>
Have more questions? Submit a request

0 Comments

Article is closed for comments.