How to write a PHP function that will give two result. Please check my code -
i'm practising codility test. , came accross question, have working code test getting error when tried use 2 solution in 1 function.

for test n = 213 code below works/correct.
// solution 213 function solution($n) { // test 213 $n = count($n); $test1 = ($n + 213) * ($n + 321) / 213 - 2; for($i = 0; $i < $n; $i++){ $test1 -= $n[$i]; } return intval($test1); //result 213 } for test n = 553 code works/correct.
// solution 553 function solution($n) { // test 553 $n = count($n); $test2 = ($n + 553) * ($n + 355) / 355 - 2; for($i = 0; $i < $n; $i++){ $test2 -= $n[$i]; } return intval($test2); // 553 } my problem how write function 2 result, when tried code error:
function solution($n) { // test 213 $n = count($n); $test1 = ($n + 213) * ($n + 321) / 213 - 2; for($i = 0; $i < $n; $i++){ $test1 -= $n[$i]; } return intval($test1); //result 213 // test 553 $n = count($n); $test2 = ($n + 553) * ($n + 355) / 355 - 2; for($i = 0; $i < $n; $i++){ $test2 -= $n[$i]; } return intval($test2); //result 553 } 
after "return", function end , won't able return else. functions meant return 1 result. if want more "results" - wrap them array. instance:
function smth() { $results = array(); $results['firstnumber'] = 1; $results['secondnumber'] = 2; return $results; } or
function smth() { $results = array(); // make numeric array $results[] = 1; $results[] = 2; return $results; }
Comments
Post a Comment