gardan06 Posted September 8, 2006 Share Posted September 8, 2006 is it possible for a function to return multiple values? if so, how? Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/ Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 You can return multiple values by using an array.Example:[code]<?phpfunction multret($a,$b) { return(array($a,$b,$a+$b,$a-$b,$a*$b,$a/$b));}list ($orig1,$orig2,$add,$sub,$mult,$div) = multret(rand(10,100),rand(10,100));?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88565 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 $orig1 should be the value of $a, $orig2 would be $b, etc right?i tried it in my file, didnt work.. Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88578 Share on other sites More sharing options...
onlyican Posted September 8, 2006 Share Posted September 8, 2006 works for meWhat version of php are u runningalso notethe array does not have keysarray(1 => "This", 2 => "that")does not work Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88581 Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 Yes.Please post the code that didn't work.Here's an example of code the does work;[code]<?phpfunction multret($a,$b) { return(array($a,$b,$a+$b,$a-$b,$a*$b,$a/$b));}list ($orig1,$orig2,$add,$sub,$mult,$div) = multret(rand(10,100),rand(10,100));echo $orig1 . ' + ' . $orig2 . ' = ' . $add . '<br>';echo $orig1 . ' - ' . $orig2 . ' = ' . $sub . '<br>';echo $orig1 . ' * ' . $orig2 . ' = ' . $mult . '<br>';echo $orig1 . ' / ' . $orig2 . ' = ' . $div . '<br>';?>[/code]Ken(* Post #3000 :) *) Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88582 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 function openxml($link) { $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($link,"r"))) { die ("could not open RSS for input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); $title_ar = ""; $desc_ar = ""; for ($j=0;$j<sizeof($arItems);$j++) { $txItem = $arItems[$j]; $title_ar .= addslashes(trim($txItem->xTitle)); $desc_ar .= addslashes(trim($txItem->xDescription)); //echo "title = ".htmlspecialchars(trim($title_ar))."<br>link = ".addslashes(trim($txItem->xLink))."<br>desc = ".htmlspecialchars(trim($desc_ar))."<p>"; } return(array(htmlspecialchars(trim($title_ar)), htmlspecialchars(trim($desc_ar))));}list ($feed_title, $feed_desc) = openxml($link);echo "<p>feed_url = ".$link."<br>title = ".$feed_title."<br>desc = ".$feed_desc;only $link has a value. $feed_title and $feed_desc are blank. Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88598 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 no reply sir? Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88659 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 [quote author=kenrbnsn link=topic=107376.msg430775#msg430775 date=1157738197]You can return multiple values by using an array.Example:[code]<?phpfunction multret($a,$b) { return(array($a,$b,$a+$b,$a-$b,$a*$b,$a/$b));}list ($orig1,$orig2,$add,$sub,$mult,$div) = multret(rand(10,100),rand(10,100));?>[/code]Ken[/quote]i did this code on my php file and it worked. i coded the same to my php file and i was wondering why it didnt show the same results as your code did.. Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88667 Share on other sites More sharing options...
onlyican Posted September 8, 2006 Share Posted September 8, 2006 try chaning return(array(htmlspecialchars(trim($title_ar)), htmlspecialchars(trim($desc_ar))));to $title_ar = htmlspecialchars(trim($title_ar));$desc_ar = htmlspecialchar(trim($desc_ar));return array($title_ar, $desc_ar); Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88669 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 still nothing.. am i doing the code right? i mean if i dont include it in a function, it works, but if i do, it wouldnt work.. Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88671 Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 Put some echo statements in your function to see if you're getting any data into the variables.Ken Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88678 Share on other sites More sharing options...
gardan06 Posted September 8, 2006 Author Share Posted September 8, 2006 [quote author=kenrbnsn link=topic=107376.msg430901#msg430901 date=1157750483]Put some echo statements in your function to see if you're getting any data into the variables.Ken[/quote]echo inside the function also inputs empty.i just compromised by NOT making a function..thanks though.. Link to comment https://forums.phpfreaks.com/topic/20146-about-functions/#findComment-88689 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.