foreach statements - can you help?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • siu02rk

    #1

    foreach statements - can you help?

    Hi all

    I cant itterate through an array! Why doesn't this code work?

    <html>
    <title> Hello World Script </title>
    <body>
    <?php
    $Name="Joe Blogg";
    $a=a;
    $b=b;
    $rabbit[0]="Test";
    $rabbit[1]="The";
    $rabbit[2]="Array";

    echo "Hello World";

    echo "<p> My name is "."$Name </p>";

    echo $a." ".$b;

    echo "$rabbit[0] $rabbit[1] $rabbit[2]";

    echo "<p>";

    for ($i=1;$i<13;$i+ +)
    {
    echo "\n Multiply by $i : ";
    echo "<p>";
    for($e=1;$e<13; $e++)
    {
    $result = $i * $e;
    echo "$i x $e = $i * $e";
    echo "<p>";
    }
    }

    /* for(p=0;p<3;p++ )
    {
    echo $rabbit[$p];
    }
    */

    foreach($k as $rabbit);
    {
    echo "$k";
    }

    ?>
    </body>
    </html>


    Thanks in advance for your help.
  • kingofkolt

    #2
    Re: foreach statements - can you help?

    "siu02rk" <siu02rk@hotmai l.com> wrote in message
    news:823c05f1.0 409111610.24b80 057@posting.goo gle.com...[color=blue]
    > Hi all
    >
    > I cant itterate through an array! Why doesn't this code work?
    >
    > <html>
    > <title> Hello World Script </title>
    > <body>
    > <?php
    > $Name="Joe Blogg";
    > $a=a;
    > $b=b;
    > $rabbit[0]="Test";
    > $rabbit[1]="The";
    > $rabbit[2]="Array";
    >
    > echo "Hello World";
    >
    > echo "<p> My name is "."$Name </p>";
    >
    > echo $a." ".$b;
    >
    > echo "$rabbit[0] $rabbit[1] $rabbit[2]";
    >
    > echo "<p>";
    >
    > for ($i=1;$i<13;$i+ +)
    > {
    > echo "\n Multiply by $i : ";
    > echo "<p>";
    > for($e=1;$e<13; $e++)
    > {
    > $result = $i * $e;
    > echo "$i x $e = $i * $e";
    > echo "<p>";
    > }
    > }
    >
    > /* for(p=0;p<3;p++ )
    > {
    > echo $rabbit[$p];
    > }
    > */
    >
    > foreach($k as $rabbit);
    > {
    > echo "$k";
    > }
    >
    > ?>
    > </body>
    > </html>
    >
    >
    > Thanks in advance for your help.[/color]

    In your first (commented out) iteration of $rabbit, you left out the $
    symbol in the for() statement. for(p=0;p<3;p++ ) should be
    for($p=0;$p<3;$ p++).

    In your second iteration (the foreach() one), you gave the foreach()
    "parameters " in the wrong order. It should be foreach($rabbit as $k). Also,
    take away the semicolon at the end of that line.

    - JP


    Comment

    • Alvaro G. Vicario

      #3
      Re: foreach statements - can you help?

      *** siu02rk escribió/wrote (11 Sep 2004 17:10:11 -0700):[color=blue]
      > foreach($k as $rabbit);
      > {
      > echo "$k";
      > }[/color]

      Figuring out what arguments functions expect makes coding unnecessarily
      difficult. That's why there's a manual that explains it ;-)



      --
      -+ Álvaro G. Vicario - Burgos, Spain
      +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
      ++ Las dudas informáticas recibidas por correo irán directas a la papelera
      -+ I'm not a free help desk, please don't e-mail me your questions
      --

      Comment

      Working...