Along time ago I wrote this program that prime factors a number. Under EasyPHP 1.8 certain numbers crash the program. The program crashes even if the first thing it does is print out a line it won't do this. An example number is 15999. This was one of the first programs I wrote and my style was very poor, this is a prime example of spaghetti code.
<?php
print("started");
$num = 15999;
$startnum = 1;
if($num == 1){
print("1 has no factors");
exit;
}
if($num == 0){
print("0 has no factors");
exit;
}else{
print("The factors of ".$num." are:<br />");
}
primefind($num, $startnum);
if($num == 0){
print("0");
}
else if($num == 1){
print("1");
}else{
}
function primefind($num, $startnum){
$primestat = 'f';
for($counter1 = $startnum; $counter1<=$num; $counter1++){
for($counter2 = 2; $counter2<$counter1; $counter2++){
$primecheck = $counter1%$counter2;
if($primecheck != 0){
$primestat = 't';
}else{
$primestat = 'f';
break;
}
}
if($primestat == 't'||$counter1 == 2){
factorcheck($counter1, $num);
break;
}
}
}
function factorcheck($prime, $num){
$remainder = $num%$prime;
if($remainder == 0)
{
print($prime.'<br />');
$startnum = 1;
primefind(($num/$prime), $startnum);
//exit;
return $prime;
}
else{
$prime++;
primefind($num, $prime);
}
}
?>
Does anyone know why certain numbers crash this program? How does PHP work, how is it possible for a script to crash immediately without printing the first line?