Daniel
Daniel

Reputation: 127

Return value from PHP function

what is wrong with the following code? Hope you understand what I'm trying to do. I'm not very familiar with functions.

function test ($variable) {
   $one = 3;
   if ($variable == 10) {
       $one = "2";
   }
   return $one;
}

foreach ($array as $arraypart) { 
   $part = explode(',',$arraypart);
   test($part[0]);
   echo $one;
}

Upvotes: 0

Views: 1474

Answers (2)

Rukmi Patel
Rukmi Patel

Reputation: 2561

here what happens, your function is being called but no variable is there to catch what test() is returning....

you need catch value which is returned by temp func like this

$val = temp($part[0]); or you can write direct echo temp($part[0]);

Upvotes: 0

Gedrox
Gedrox

Reputation: 3612

You need to assign the result of function to a variable:

$one = test($part[0]);

Upvotes: 5

Related Questions