eldan88 Posted September 1, 2013 Share Posted September 1, 2013 Hey guys. I am trying to create a method that will return the variable and whats assigned to the vairable. Not jsut the string. So for example, when I call my code account() below, I want it to return the variables and whats assigned to them, so that I can work with these variables. function accounts($demo = TRUE) { if($demo) { $from_number = "55555555555"; $sid = "******************"; $token = "*****************"; } else { $from_number = "5555555555"; $sid = "*****************"; $token = "*****************"; } } accounts(); Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 1, 2013 Share Posted September 1, 2013 (edited) Not sure I totally understand, but try this and let us know: function accounts($demo = TRUE) { $from_number = "55555555555"; $sid = "******************"; $token = "*****************"; if($demo) { return compact('from_number', 'sid', 'token'); } else { //whatever } } print_r(accounts()); Edited September 1, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted September 1, 2013 Share Posted September 1, 2013 Hey guys. I am trying to create a method that will return the variable and whats assigned to the vairable. Not jsut the string. So for example, when I call my code account() below, I want it to return the variables and whats assigned to them, so that I can work with these variables. function accounts($demo = TRUE) { if($demo) { $from_number = "55555555555"; $sid = "******************"; $token = "*****************"; } else { $from_number = "5555555555"; $sid = "*****************"; $token = "*****************"; } } accounts(); You already answered the question yourself. Again "a method that will return the variable and whats assigned to the vairable". Im sorry but you seem not to understand how to carry out your objective. Rather you are going around with what you think you know or can work. Simply supply the variables as arguements that will return their values when involed. Why not: function accounts($demo=true,$from_number,$sid,$token) { if($demo) { if {$from_number = "blah"; $sid = "******************"; $token = "*****************"; } else { $from_number = "5555555555"; $sid = "*****************"; $token = "*****************"; } } accounts($demo=true,$from_number,$sid,$token); You might hav to still debug further...to get there. Quote Link to comment Share on other sites More sharing options...
Irate Posted September 1, 2013 Share Posted September 1, 2013 What AbraCadaver posted is quite a good example. compact() is quite what you want. Check the official document of compact() and then think how to use the script again. AbraCadaver possibly covered it all, just pointing out what he did. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 2, 2013 Share Posted September 2, 2013 Just return an object. Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 2, 2013 Author Share Posted September 2, 2013 Not sure I totally understand, but try this and let us know: function accounts($demo = TRUE) { $from_number = "55555555555"; $sid = "******************"; $token = "*****************"; if($demo) { return compact('from_number', 'sid', 'token'); } else { //whatever } } print_r(accounts()); Hey AbraCadaver! Thanks for the reply again. I think i didn't state my question properly. What I want to do return that actual variables and values and pass them into a function so if i call the account() function all the varibales and values will be visible, so that i can pass them into this function below make_call($demo_sid,$demo_token,$from_number,$to_number,$confirmation_method); Quote Link to comment Share on other sites More sharing options...
eldan88 Posted September 2, 2013 Author Share Posted September 2, 2013 (edited) Just return an object. You mean create class and return an object?? Edited September 2, 2013 by eldan88 Quote Link to comment Share on other sites More sharing options...
objnoob Posted September 2, 2013 Share Posted September 2, 2013 (edited) You have a few options... You can work with global variables (not recommended), you can accept function parameters as references instead of values, or you can return an array of values. Here is how to work with references.... <?php // my variables that will be referenced $variable1 = 'a string'; $var2 = 13; // a number $var3 = new someObject(); // my function that will accept parameters by reference and not by value function myFunctionUsesReferences(&$param1, &$param2, &$param3){ $param1 = 'a new string'; $param2 = 14; $param3->setId(45); } // the arguments $variable1, $var2, $var3 are passed to the function by reference and not by value myFunctionUsesReferences($variable1, $var2, $var3); Edited September 2, 2013 by objnoob Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 3, 2013 Share Posted September 3, 2013 (edited) You mean create class and return an object?? No, I mean create a dynamic object. function getAccount($demo = TRUE) { $account = new stdClass(); if($demo) { $account->from_number = "55555555555"; $account->sid = "******************"; $account->token = "*****************"; } else { $account->from_number = "5555555555"; $account->sid = "*****************"; $account->token = "*****************"; } return $account; } $account = getAccount(); var_dump($account); Result stdClass Object ( [from_number] => 55555555555 [sid] => ****************** [token] => ***************** ) Edited September 3, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted September 3, 2013 Share Posted September 3, 2013 I'm still fuzzy on your intent, but maybe just build an array and then use it to call the other function. Also, maybe $demo confused me and that's not part of your question? function accounts($demo = TRUE) { $result['from_number'] = "55555555555"; $result['sid'] = "******************"; $result['token'] = "*****************"; return $result; } $a = accounts(); make_call($a['sid'], $a['token'], $a['from_number'], $to_number, $confirmation_method); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.