简体   繁体   中英

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;
}

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

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

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM