Hey everybody this is my first post here and just learning PHP and need some help on an infinite loop problem. The code is supposed to find how many ways you can change a $1 dollar bill into coins using dollar coin, half dollars, quarters, dimes, nickels, and pennies. But i am having a problem because it is timimg out my browser and i asked my instructor and he said i have a infinite loop. I honestly dont have a clue what to look what when i comes to this.

Heres the code:

<?php
	
//Setting all coins to 0
    $loopCounts = 0;
    $numDollars = 0;  	  
    $numHalfs  = 0;     
    $numQuarters = 0;   	
    $numDimes  = 0;    
    $numNickels   = 0;   
    $numPennies   = 0;   
    
    //What the max number of each coin can be to make a $1.00
    $maxDollars  =   1;
    $maxHalfs    =   2;
    $maxQuarters =   4;
    $maxDimes    =  10; 
    $maxNickels  =  20; 
    $maxPennies  = 100; 
 	
   //The value of what each coin is worth
    $valDollar  = 100;
    $valHalf    =  50;
    $valQuarter =  25;
    $valDime    =  10;
    $valNickel  =   5;
    $valPenny   =   1;
	
	
    print("<table border=\"2\"> ");
print("<tr><td>Number</td><td><strong>Dollars</strong></td><td>
<strong>HalfDollars</strong></td><td><strong>Quarters</strong></td><td><strong>Dimes</strong></td><td><strong>Nickels</strong></td><td><strong>Pennies</strong></td></tr>");
	
//Beginning of the loops
	FOR ($numDollars =0; $numDollars <= $maxDollars; $numDollar++)
	{
		FOR ($numHalfs =0; $numHalfs <= $maxHalfs; $numHalfs++)
		{
			FOR ($numQuarters =0; $numQuarters <= $maxQuarters; $numQuarters++)
			{
				FOR ($numDimes =0; $numDimes <= $maxDimes; $numDimes++)
				{
					FOR ($numNickels =0; $numNickels <= $maxNickels; $numNickels++)
					{
						FOR ($numPennies =0; $numPennies <= $maxPennies; $numPennies++)
						{
							$loopCounts = $loopCounts + 1;
							
							$coinSum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny);
									   
							IF ($coinSum == 100)
							{
								$goodCombo = $goodCombo + 1;
								print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>");
							}//end if
						}//end FOR loop for Pennies
					}//end FOR loop for Nickels
				}//end FOR loop for Dimes
			}//end FOR loop for Quarters
		}//end FOR loop for Half Dollars
	}//end FOR loop for Dollar Coin
	
	print("<p>There are $loopCounts you can make change for One Dollar</p>");
	print("</table>");
	
	?>

It would be a big help if yall could help me look for it because I have know idea what to look for. Thank you for your time.

i had an idea that the way you positioned the for loops was making it repeat itself way too many times. so i reversed the setup and it worked. it was kind of a guess to be honest, but an educated one at least. loops like that can really mess with your head when trying to figure them out.

<?php

	//Setting all coins to 0
    $loopCounts = 0;
    $numDollars = 0;  	  
    $numHalfs  = 0;     
    $numQuarters = 0;   	
    $numDimes  = 0;    
    $numNickels   = 0;   
    $numPennies   = 0;   
    //What the max number of each coin can be to make a $1.00
    $maxDollars  =   1;
    $maxHalfs    =   2;
    $maxQuarters =   4;
    $maxDimes    =  10; 
    $maxNickels  =  20; 
    $maxPennies  = 100; 
    //The value of what each coin is worth
    $valDollar  = 100;
    $valHalf    =  50;
    $valQuarter =  25;
    $valDime    =  10;
    $valNickel  =   5;
    $valPenny   =   1;

print("<table border=\"2\"> ");
print("<tr><td>Number</td><td><strong>Dollars</strong></td><td>
<strong>HalfDollars</strong></td><td><strong>Quarters</strong></td><td><strong>Dimes</strong></td><td><strong>Nickels</strong></td><td><strong>Pennies</strong></td></tr>");
	for ( $numPennies = 0; $numPennies <= $maxPennies; $numPennies++ ) {
		for ( $numNickels = 0; $numNickels <= $maxNickels; $numNickels++ ) {
			for ( $numDimes = 0; $numDimes <= $maxDimes; $numDimes++ ) {
				for ( $numQuarters = 0; $numQuarters <= $maxQuarters; $numQuarters++ ) {
					for ( $numHalfs = 0; $numHalfs <= $maxHalfs; $numHalfs++ ) {
						for ( $numDollars = 0; $numDollars <= $maxDollars; $numDollars++ ) {
							$sum = ($numDollars * $valDollar) + ($numHalfs * $valHalf) + ($numQuarters * $valQuarter) + ($numDimes * $valDime) + ($numNickels * $valNickel) + ($numPennies * $valPenny);
							if ( $sum == 100 ) {
								$goodCombo = $goodCombo + 1;
								print("<tr><td>$goodCombo</td><td>$numDollars</td><td>$numHalfs</td><td>$numQuarters</td><td>$numDimes</td><td>$numNickels</td><td>$numPennies</td></tr>");
							}
						}
					}
				}
			}
		}
	}
	print("<p>There are $goodCombo you can make change for One Dollar</p>");
	print("</table>");

?>

also, there are 293 ways according to the script, in which was proven by a simple google search, so your code works.

Thanks bossman I really appreciate it!!! The code now works wonders and I also found from comparing code that my $numDollar++ was not $numDollars++, which is what it needed to be haha. But anyways thanks again for your time boss

Nice catch there. I didn't see that. maybe if I examined the code some more i could of found that without rewriting the code. so it didn't have to reversed after all. That probably should of been the first thing i looked at.

Ahh you straight man, I still appreciate your help man. Well come here for all of my help in coding and yall are very qucik on your replies and are very nice. Thanks again man.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.