1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What's wrong with my code?

Discussion in 'PHP' started by zambonibutters, Aug 10, 2010.

  1. #1
    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?
     
    zambonibutters, Aug 10, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    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:
     
    themullet, Aug 10, 2010 IP
  3. zambonibutters

    zambonibutters Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    zambonibutters, Aug 10, 2010 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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.)
     
    nico_swd, Aug 10, 2010 IP
  5. zambonibutters

    zambonibutters Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    zambonibutters, Aug 10, 2010 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    you can just put the function outside the loop. as nico said you can't redeclare functions
     
    themullet, Aug 10, 2010 IP