Page 1 of 1

how to return 2 values from a function in this case:

Posted: Tue Sep 30, 2008 3:16 am
by swetha
ive written the following function for preg_match_all

Code: Select all

 
function repeat($reg,$search)
{
   $result1=preg_match_all($reg,$search,$match);
   if ($result1==0) 
    {
    $output= "value not found";
     }
  else
  {
      //echo "preg_match:result not found";
       $output="value found";
  }
 return $match[0];
}
 
inside a switch statement i assign

Code: Select all

 
$constr =repeat($reg,$search); 
 

inside a textarea i have the following code

Code: Select all

 
<textarea  name="resulttxt" readonly="readonly">
<?php if (isset($_POST['submit']))
{ 
  $i=1;
  foreach($constr as $value)
  {
      echo "Matched Value " .$i. ":" .$value;
      echo "\n";
      $i++;
  } 
}
else{echo " ";}?></textarea> 
 
my doubt is how do i return the $result1 also along with $match[0] and print its value in the textarea.

Re: how to return 2 values from a function in this case:

Posted: Tue Sep 30, 2008 9:29 am
by VladSun
Return an array fillded up with what you need.

Re: how to return 2 values from a function in this case:

Posted: Tue Sep 30, 2008 10:53 pm
by swetha
but in this case how do i return it and assign the value?

Re: how to return 2 values from a function in this case:

Posted: Tue Sep 30, 2008 10:57 pm
by Luke
Are you trying to do something like this?

Code: Select all

function twoValues() {
    return array('value1', 'value2');
}
 
list($value1, $value2) = twoValues();

Re: how to return 2 values from a function in this case:

Posted: Wed Oct 01, 2008 4:03 am
by swetha
Im having following 2 functions repeat1 and repeat2.
In the first case,i have an array which is suppose to return 2 values.one is the number of matched values and the other one is the contents of match[0].But im not getting the reuqire output.I have displayed the ouput that i am getting.The desired output is displayed in the end.Kindly help

Code: Select all

 
<?php
echo "function repeat1" . "<br/>";
function repeat1($reg,$search)
{
    $result1=preg_match_all($reg,$search,$match);
    if ($result1==0) 
    {
          $output= "value not found";
    }
    else
    {
         //echo "preg_match:result not found";
          $output="value found";
    }
    echo "repeat1:" .$output;
    //return $match[0];
    $retarray=array($result1,$match[0]);  
    return $retarray;
}   
$assign=repeat1("/[a-z]/","asfaqwqeq"); 
echo "<br/>";
foreach($assign as $disp)
{
   echo  "return from repeat1:".$disp . "<br/>";                            
}       
 

Code: Select all

 
echo "function repeat2" . "<br/>";
function repeat2($reg,$search)
{
    $result1=preg_match_all($reg,$search,$match);
    if ($result1==0) 
    {
          $output= "value not found";
    }
    else
    {
         //echo "preg_match:result not found";
         $output="value found";
    }
    //return $match[0];
    echo "repeat2:" .$output . "<br/>";
    return $match[0];
}   
$assign=repeat2("/[a-z]/","asfaqwqeq"); 
foreach($assign as $disp)
{
   echo "return from repeat2:".$disp . "<br/>";                         
}                                                                        
?>
 
Output1:
function repeat1
repeat1:value found
return from repeat1:9
return from repeat1:Array




Output2
function repeat2
repeat2:value found
return from repeat2:a
return from repeat2:s
return from repeat2:f
return from repeat2:a
return from repeat2:q
return from repeat2:w
return from repeat2:q
return from repeat2:e
return from repeat2:q


This is what i require
From the first function ,i need the following output:

function repeat1
repeat1:value found
return from repeat1:9
return from repeat1:a
return from repeat1:s
return from repeat1:f
return from repeat1:a
return from repeat1:q
return from repeat1:w
return from repeat1:q
return from repeat1:e
return from repeat1:q


i.e the main problem am having is that am unable to display the value of match[0] in function
repeat1