severndigital Posted October 12, 2009 Share Posted October 12, 2009 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 Link to comment https://forums.phpfreaks.com/topic/177428-access-a-functions-return-without-assigning-a-variable/ Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 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); Link to comment https://forums.phpfreaks.com/topic/177428-access-a-functions-return-without-assigning-a-variable/#findComment-935513 Share on other sites More sharing options...
Skepsis Posted October 12, 2009 Share Posted October 12, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177428-access-a-functions-return-without-assigning-a-variable/#findComment-935517 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.