Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Location
    Manchester
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assigning value to auto increment variable

    hi all

    first post and hopefully someone can help this has been doing my head in for last 24 hours , decide to appeal to the wonderweb to see if I can find someone much smarter than myself willing to point us in the right direction.

    Any idea on how to assign value to a auto increment variable inside a loop .lets say for example


    $letter="a";
    for($i=1;$i<=5;$i++){
    $letter = $i;
    $letter++;
    }

    i want output in this way;

    $a=1
    $b=2
    $c=3
    $d=4
    $e=5

    or any alternative to this?

    Thanks in advance

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    The real question is, "what is your goal, or purpose for doing this?"

    Technically it can be done, but programming is based on "best principles",
    making a script fast, efficient, and a non-burden for a processor.

    What you might actually be thinking about is an array ...

    $a[0] ... to .... $a[1000]

    That would be 1001 unique values (variables) that are easily manipulated.

    Tell us what your goal is, or what you're attempting to do.



    .

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    edit.
    Last edited by Arcticwarrio; 08-10-2012 at 02:41 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    sorry, i got it the wrong way round:

    this is better:

    PHP Code:
    <?php
    $letters 
    = array("","a","b","c","d","e");
    for(
    $i=1;$i<=5;$i++){
        $
    $letters[$i] = $i;
    }
    echo 
    '<br>a = '.$a;
    echo 
    '<br>b = '.$b;
    echo 
    '<br>c = '.$c;
    echo 
    '<br>d = '.$d;
    echo 
    '<br>e = '.$e;
    ?>
    Last edited by Arcticwarrio; 08-10-2012 at 02:40 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Maybe you're talking about "variable variables"?
    http://php.net/manual/en/language.va...s.variable.php

    Should they be used?
    In my opinion, never.



    .

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I agree; I've yet to find a situation where variable variables are a requirement and there is always a way around them. They are more of a headache in keeping track of them.
    Use arrays. If you can be clearer on the actual purpose, we can make better examples. Given what you have done here, a simple:
    PHP Code:
    $aKeys range("a""e");
    $aValues range(1count($aKeys));
    $aResult array_combine($aKeys$aValues); 
    Would generate an associative array of the letters with the numbers.

  • Users who have thanked Fou-Lu for this post:

    Arcticwarrio (08-10-2012)

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    thanks to fou-lu for that as i have learnt about array_flip(); today lol (by googleing range())
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

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