PHP array values not being changed -
i trying make program synthetic division requires factoring wrote function factor integer works, never changes values of php array $factors. any appreciated.
$factors=array(); $i; function factor($x){ if($x==0){ echo "(0,0)"; } else { $n=false; if($x<0) { $x=abs($x); $n=true; } for($i=2; $i<=$x; $i++) { if($x%$i==0){ if($n){ $factors[(count($factors))]=(-1*($x/$i)); $factors[(count($factors))]=($i); $factors[(count($factors))]=($x/$i); $factors[(count($factors))]=(-1*$i); } else { $factors[(count($factors))]=($x/$i); $factors[(count($factors))]=($i); } } } } } factor(-4);
try this
function factor($x){ $factors=array(); if($x==0){ echo "(0,0)"; } else { $n=false; if($x<0) { $x=abs($x); $n=true; } for($i=2; $i<=$x; $i++) { if($x%$i==0){ if($n){ $factors[(count($factors))]=(-1*($x/$i)); $factors[(count($factors))]=($i); $factors[(count($factors))]=($x/$i); $factors[(count($factors))]=(-1*$i); } else { $factors[(count($factors))]=($x/$i); $factors[(count($factors))]=($i); } } } //end echo '(' . implode(',', $factors). ')'; } //end if $x == 0 } factor(-4);
$factors
outside scope of function, besides not returning or outputting anything. seeing echo (0,0)
assumed wanted echoed such (2,4,8)
etc. can implode..
Comments
Post a Comment