Jump to content

Need help displaying variables through functions


eldan88

Recommended Posts

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();

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 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.

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.

 

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);

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);


      

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] => *****************
)

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.