SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow How to make this code shorter using looping the array?

    Ok guys

    I have this codes below which has the variable name '$tmpbackcolor1' from 1 to 100, which is very annoying because it's too long.

    Code:
    $tmpbakcolor1="#FF0000";
    $tmpbakcolor2="#FF0000";
    $tmpbakcolor3="#FF0000";
    $tmpbakcolor4="#FF0000";
    $tmpbakcolor5="#FF0000";
    
    ...
    
    $tmpbakcolor99="#FF0000";
    $tmpbakcolor100="#FF0000";
    Now how do i convert it into array variables and just loop it to make it shorter.

    this easy for coder who is very familiar with array and some loop functions.

    Your help is a big enhancement to my codes.
    therefore thank you in advance.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    for($count = 1; $count < 101; $count ++) {
      $var = '$tmpbakcolor' . $count;
    
      $$var = '#FF0000';
    }
    Give that a go

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks poizn but where is the array variable?

    ok, i'll try that anyway.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $a array_fill1100'#F00' ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks poizn but where is the array variable?
    What do you mean array variables? Do you want to create an array? Maybe I misinterpreted the question...

    My way will create all the variables $tmpbakcolor1 to $tmpbakcolor100.
    So you can just use the variables like that

    Code:
    for($count = 1; $count < 101; $count ++) {
      $var = '$tmpbakcolor' . $count;
    
      $$var = '#FF0000';
    }
    
    echo 'variable 1, named "tmpbakcolor1", has value' , $tmpbakcolor1 , '<br />';

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use a standard array?

    PHP Code:
    $tmpbackcolor = array();

    for (
    $i 0$i 100$i++) {
        
    $tmpbackcolor[] = "#FF0000";

    So, tmpbackcolor[99] is the 100th element, as arrays start at 0.

  7. #7
    SitePoint Zealot
    Join Date
    May 2006
    Location
    Jhyalipati,Chapagaun-9, Lalitpur, Bagmati, Nepal
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do according to logic_earth
    PHP Code:
    $tmpbakcolor array_fill1100'#FF0000' ); 
    print_r($tmpbakcolor); 
    Simply Sajan

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why would you want 100 variables all containing the same color?

    Explain why you want that and I bet we can offer better advice than this.

    I am sure you only want some of them to be that color and others to be different colors.

  9. #9
    SitePoint Zealot
    Join Date
    May 2006
    Location
    Jhyalipati,Chapagaun-9, Lalitpur, Bagmati, Nepal
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what you do cups for this requirement.
    How to make this code shorter using looping the array?
    Ok guys

    I have this codes below which has the variable name '$tmpbackcolor1' from 1 to 100, which is very annoying because it's too long.

    Code:

    $tmpbakcolor1="#FF0000";
    $tmpbakcolor2="#FF0000";
    $tmpbakcolor3="#FF0000";
    $tmpbakcolor4="#FF0000";
    $tmpbakcolor5="#FF0000";

    ...

    $tmpbakcolor99="#FF0000";
    $tmpbakcolor100="#FF0000";

    Now how do i convert it into array variables and just loop it to make it shorter.

    this easy for coder who is very familiar with array and some loop functions.

    Your help is a big enhancement to my codes.
    therefore thank you in advance.
    Simply Sajan

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hi Sajan, not really sure what your comments mean ...

    I am questioning why someone wants ~100 vars with the same value, partially just to stretch my own understanding really ... but if we can help the OP then that would be great too.


Bookmarks

Posting Permissions

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