Jump to content

access a functions return without assigning a variable


severndigital

Recommended Posts

i have a function something like this

 

function myfunction($var,$var2){

//bunch of code here
//that results in a

return array();
}

 

I can access that array without assign a variable?

 

something like

 

myfunction("input1","input2")[0];

 

or am I looking for something that just isn't possible?

 

Thanks in advance,

C

No, you need to assign to a variable, and then reference the element within that variable, unless you do something like:

function myfunction($var,$var2,$n=-1){
   //bunch of code here
   //that results in a
   $result = array();
   if ($n == -1) {
      return $result;
   } else {
      return $result[$n];
   }
}

myfunction("input1","input2",0);

if you're wanting to know how to use a custom function that returns an array it would be something like.

$user_info = get_user_info($user_id);

 

Then you can use, $user_info['array_key'], ex: $user_info['username'];

The function would have to return an array, so basically:

function get_user_info($user_id){   $query = mysql_query("SELECt * FROM users");   while ($data = mysql_fetch_assoc($query))   {        return $data;   }}

Good luck, if you need any deeper explanation I can do so.

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.