0

I was trying to make a program to do synthetic division which requires factoring so I wrote this function to factor an integer which works, but it never changes the values of the php array $factors. Any help will be greatly 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);
  • Where is the return? they are being changed, but your not returning anything. And your array `$factors` is outside the functions scope. – ArtisticPhoenix Sep 13 '16 at 03:59
  • that's because `$factors` is outside the function. if you want to directly modify it in a function without either returning it or passing the memory address you need to use [`global`](http://php.net/language.variables.scope) in the function to manipulate it's scopre – Memor-X Sep 13 '16 at 04:01
  • global is so ugly .... – ArtisticPhoenix Sep 13 '16 at 04:03
  • @ArtisticPhoenix i know, it's more of a convenient hack for lazy programming/ers – Memor-X Sep 13 '16 at 04:05
  • @Memor-X - yes but I have to admit I did find one valid use for it in my framework ( that I am making ) and that is as the environment ( such as production, debug ). I thought of using a constant such as CI does, but then I cant change it on a page for page basis ... lol ... but I don't write functions either. – ArtisticPhoenix Sep 13 '16 at 04:10

1 Answers1

0

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 for
       echo '(' . implode(',', $factors). ')';
    } //end if $x == 0
}
factor(-4);

$factors is outside the scope of the function, besides that you were not returning or outputting anything. Seeing as you echo (0,0) I assumed you wanted it echoed such as (2,4,8) etc. Which you can do with implode..

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38