Okay, so here's the code: <html> <head> </head> <body> <?php function placemake($posit) { echo $posit . " place.<br />"; } for ($x=1; $x<10; $x++) { $t="You came in "; echo $t; placemake("1st"); echo "$t"; placemake("2nd"); echo "$t"; placemake("3rd"); function mult($y,$z) { $result=$z*$y; return $result; } echo "234x128=" . mult(234,128) . "<br>"; } ?> </body> </html> The problem is that I want it to repeat the following lines until $x=10: You came in 1st place. You came in 2nd place. You came in 3rd place. 234x128=29952 Instead, however, it only goes through 1.5 loops. Why?
problem with redeclaring of function works with <html> <head> </head> <body> <?php function placemake($posit) { echo $posit . " place.<br />"; } for ($x=1; $x<10; $x++) { $t="You came in "; echo $t; placemake("1st"); echo "$t"; placemake("2nd"); echo "$t"; placemake("3rd"); echo "234x128=" . (234*128) . "<br>"; } ?> </body> </html> PHP:
Well, ou're right. That works perfectly. But what was wrong with using: function mult($y,$z){ $result=$z*$y; return $result; } echo mult(234,128) Why didn't it work in the original code?
Because you declared the function inside the loop. In PHP, you cannot re-declare or override functions, meaning, if there's already a function with that name, it'll throw an error. You can move the function outside the loop, or you can use themullet's version. (Although, quite honestly, I don't think you need a function to do simple math like that.)
Oh, I know that. I'm just trying to learn php and I want to know how to loop functions and arrays like this without coming across errors like these when I begin working on a larger scale.