Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2003
    Posts
    26

    Unanswered: using eval() or any otherway to get a value of something lik

    have parms that are sent to the server
    i want to read them ina loop
    like this
    for($i=0;$i<10;++$i){

    echo eval('date'+$i);
    }

    but it isnt working!
    why?
    thanks in advance
    peleg

  2. #2
    Join Date
    Jan 2004
    Posts
    6

    Re: using eval() or any otherway to get a value of something lik

    Originally posted by pelegk2
    have parms that are sent to the server
    i want to read them ina loop
    like this
    for($i=0;$i<10;++$i){

    echo eval('date'+$i);
    }

    but it isnt working!
    why?
    thanks in advance
    peleg
    Do you mean from a form? Or something that you have attached to the URL?

    Just did this up.. hope this is what you meant..

    PHP Code:
    <?php

    $date0 
    1;
    $date1 10;
    $date2 20;
    $date3 30;
    $date4 40;
    $date5 50;
    $date6 60;
    $date7 70;
    $date8 80;
    $date9 90;


    for (
    $i 0$i 10$i++)
        {
        
    $varName 'date' $i;
        eval (
    "\$current = $$varName;");
        echo 
    $current;
        echo 
    "<br>";
        }

    //Or you can use this without using eval()
    //for ($i = 0; $i < 10; $i++)
    //    {
    //    $varName = 'date' . $i;
    //    echo $$varName;
    //    echo "<br>";
    //    }
    ?>
    Last edited by killerkooki; 01-20-04 at 00:02.

  3. #3
    Join Date
    Nov 2003
    Posts
    26

    very nice but why :

    in the code :
    //Or you can use this without using eval()
    //for ($i = 0; $i < 10; $i++)
    // {
    // $varName = 'date' . $i;
    // echo $$varName;
    // echo "<br>";
    // }


    u do twice $$ ---->>>$$varName

  4. #4
    Join Date
    Jan 2004
    Posts
    6

    Re: very nice but why :

    Originally posted by pelegk2
    in the code :
    //Or you can use this without using eval()
    //for ($i = 0; $i < 10; $i++)
    // {
    // $varName = 'date' . $i;
    // echo $$varName;
    // echo "<br>";
    // }


    u do twice $$ ---->>>$$varName
    $$ is not an error. You don't see it much but it is actually a part of php.
    It means a "Variable Variable".

    You can see more on it in the PHP Manual.
    http://www.php.net/manual/en/languag...s.variable.php

  5. #5
    Join Date
    Nov 2003
    Posts
    26

    ok

    thanks alot anyway

  6. #6
    Join Date
    Nov 2003
    Posts
    26

    does this men :

    <?php
    $a = "hello";
    ?>

    <?php
    $$a = "world";
    ?>


    <?php
    echo "$a ${$a}";
    ?>

    that there is a variable called : $world
    or i miss understand ?
    thansk inadvance
    peleg

  7. #7
    Join Date
    Jan 2004
    Posts
    6

    Re: does this men :

    Originally posted by pelegk2
    <?php
    $a = "hello";
    ?>

    <?php
    $$a = "world";
    ?>


    <?php
    echo "$a ${$a}";
    ?>

    that there is a variable called : $world
    or i miss understand ?
    thansk inadvance
    peleg
    No.
    First, you have a variable called $a. And you assign the value "hello" to it.

    Then you have another variable called $$a.
    Here the $a is interpreted to its value, which is "hello".
    So now you have $hello, and it is assigned the value "world".

    When you are printing it you print $a, which still contains the value "hello" and then you print ${$a}, which gets interpreted to $hello and contains "world".

    So it prints out "hello world".

    If you changed the value of $a after you did the assignment for $$a, printing $$a will no longer show "world".

    Hope that clears things up.

  8. #8
    Join Date
    Nov 2003
    Posts
    26

    thanks laot

    very good explenation and a clear one!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •