Jump to content


Photo

about functions..


  • Please log in to reply
11 replies to this topic

#1 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 05:52 PM

is it possible for a function to return multiple values? if so, how?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 05:56 PM

You can return multiple values by using an array.

Example:
<?php
function 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));
?>

Ken

#3 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 06:16 PM

$orig1 should be the value of $a, $orig2 would be $b, etc right?

i tried it in my file, didnt work..

#4 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 08 September 2006 - 06:19 PM

works for me
What version of php are u running
also note
the array does not have keys
array(1 => "This", 2 => "that")
does not work
Tell me the problem, I will try tell you the solution

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 06:20 PM

Yes.

Please post the code that didn't work.

Here's an example of code the does work;
<?php
function 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>';
?>

Ken

(* Post #3000 :) *)

#6 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 06:46 PM

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.

#7 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 08:45 PM

no reply sir?

#8 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 09:03 PM

You can return multiple values by using an array.

Example:

<?php
function 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));
?>

Ken

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

#9 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 08 September 2006 - 09:05 PM

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);
Tell me the problem, I will try tell you the solution

#10 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 09:11 PM

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

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 September 2006 - 09:21 PM

Put some echo statements in your function to see if you're getting any data into the variables.

Ken

#12 gardan06

gardan06
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 08 September 2006 - 09:35 PM

Put some echo statements in your function to see if you're getting any data into the variables.

Ken

echo inside the function also inputs empty.

i just compromised by NOT making a function..

thanks though..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users