-1

I have a problem in my php code that uses recursion:

<?php

solveTowers(5, "A", "B", "C");

 function solveTowers($count, $src, $dest, $spare)
    {
        if (count == 1)
        {
          echo "Move a disk from ".$src." to ".$dest ;
        }
        else
        {
            solveTowers($count - 1, $src, $spare, $dest);
            solveTowers(1, $src, $dest, $spare);
            solveTowers($count - 1, $spare, $dest, $src);
        }
    }

?>

But it doesn't run!

This error occurs:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in C:\xampp\htdocs\cap492\towers.php on line 13

Line 13 is the first call to the function in the else statment

Can you please help me with this?!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
basma
  • 1
  • 1
  • 1

1 Answers1

11

if ($count == 1) instead of if (count == 1)

Ovidiu Pacurar
  • 8,173
  • 2
  • 30
  • 36
  • 2
    Another note: ensure that the error reporting level is set to report notices as well. `count` is initially being treated as a constant, which PHP then finds doesn't exist, so it oh-so-helpfully assumes you meant the string "count". This assumption will raise a notice that could have been, well, noticed. – erisco May 25 '10 at 12:14
  • you are right :\ this happened because I translated the code from Java to PHP language now it works thank you alot – basma May 25 '10 at 16:54