Jump to content

In the class below, in function selectOut , why can't i return twice ?


jd2007

Recommended Posts

<?php

class StringManipulation
{
  
  function selectOut($string, $occur)
{ 
      $start=0;
  return $string."<br />".$occur."<br />";
  $num=strrchr($string, $occur);
  return $num;
          //return substr($string, $start, strlen($string)-$num);
}

}

$string2=new StringManipulation;
$str="Hello World";
echo $string2->selectOut($str, "o");

?>

 

this :

 

return $string."<br />".$occur."<br />";

 

returns the result i want...but this:

 

return $num;

 

returns nothing...

 

why ?

I may be mistaken, but I believe you can only use return once. It doesn't look to hard to return everything you want with one return, so you should try that. After it sends the first return, I think the function stops

once a successful return is executed, everthing else under it (in a function/method) is ignored.

 

my advice would be to return the var that meets the scope of the function, and set a class property for the other variable: $this->_var = $var;

I may be mistaken, but I believe you can only use return once. It doesn't look to hard to return everything you want with one return, so you should try that. After it sends the first return, I think the function stops

 

yahhh

Once you return, that is the end of the function. You can return only one value.

 

... but that one value that you return can be an array of many values

 

Absolutely true! You should break these out in to 2 functions.

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.