adamgonge Posted February 5, 2014 Share Posted February 5, 2014 im working on a problem that involves a loop that goes to 64 with each time the number is doubling itself so it would be 1,2,4,8,16.. I dont know how to do it . I currently have this code which is wrong it just ends up taking 64*2 but i dont know how else to set this up. <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Multiplication test</p>'; ?> <?php $total = 0; $even = 0; for ( $x = 1; $x <= 64; $x++ ) { $sum = 1+$x*2; } echo "The total sum: ".$sum."<br />"; ?> Link to comment https://forums.phpfreaks.com/topic/285954-multiplication-in-a-loop/ Share on other sites More sharing options...
adamgonge Posted February 5, 2014 Author Share Posted February 5, 2014 i changed the equation but now it just ends up with 64squared whereas i think i need the sum of all the squares. new <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Multiplication test</p>'; ?> <?php $total = 0; $even = 0; for ( $x = 0; $x < 64; $x++ ) { $sum =($x+1*2)*$x; } echo "The total sum: ".$sum."<br />"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/285954-multiplication-in-a-loop/#findComment-1467802 Share on other sites More sharing options...
Ch0cu3r Posted February 5, 2014 Share Posted February 5, 2014 Use += instead of = Also to get $x squared you need to times it by itself $sum += $x*$x; // Or use pow $sum += pow($x, 2); Link to comment https://forums.phpfreaks.com/topic/285954-multiplication-in-a-loop/#findComment-1467848 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.