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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question foreach array add part of text to each value

    I have a code that's set up like this:
    PHP Code:
    $list = array('one','two','three');

    foreach(
    $list as $number){
        $
    $number functionCall($number);

    What I need to do is add a string of text to the new variable ($$number) so it would be like this: $one_extra $two_extra $three_extra. How could i do this easily? I'm stumped and editing files at the same time as someone else on a vps (were going back in forth editing while the other isn't) so i can't just trial and error it like I usually do.

    Edit:
    Problem solved.
    Last edited by Dubz; 01-22-2012 at 08:28 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    **UPDATED**

    The way I did worked, but i have a different problem. I need to do this again, but the way i did doesn't seem to work. Say out of a basic example, I have this:
    PHP Code:
    if($_POST['value']<=0) return; 
    what i need to do is add _LIMIT again so it would be pictured something like this:
    PHP Code:
    if(${$_POST['value'].'_Limit'} <= 0) return; 
    But the above example doesn't seem to work. Any ideas?

    Edit:
    Fixed yet again -_-
    Last edited by Dubz; 01-22-2012 at 09:54 AM.

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    While I'm glad you figured your problem out, please always carefully consider your code when you're using variable variable names. Almost any time you are in that situation there's a better and less confusing way to write the code.

  • #4
    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
    Quote Originally Posted by bacterozoid View Post
    While I'm glad you figured your problem out, please always carefully consider your code when you're using variable variable names. Almost any time you are in that situation there's a better and less confusing way to write the code.
    I completely agree with this.
    Variable variables like globals should be avoided when better solutions are available (which with variable variables is always). The problem with variable variables is it promotes an unknown variable to a global variable within the scope provided. This makes for easy overwrite of existing variables, and is more true when accepting user input for the variable names.
    In the case of known sizes, extract to specific variables. In the case of unknown sizes, use arrays.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fou-Lu View Post
    I completely agree with this.
    Variable variables like globals should be avoided when better solutions are available (which with variable variables is always). The problem with variable variables is it promotes an unknown variable to a global variable within the scope provided. This makes for easy overwrite of existing variables, and is more true when accepting user input for the variable names.
    In the case of known sizes, extract to specific variables. In the case of unknown sizes, use arrays.
    I realize the problems that could occur due to this but the usage of it is meant for a bot (I have mentioned this bot before here a few times). It sets a variable to the value of the column for that bot's row so the variable wouldn't be overwritten. Also, the writing of these are almost all done in separate functions which makes it even more less possible to overwrite without setting it as a global considering each time the bot is sent the command this is in, the value is re-obtained each time.


  •  

    Posting Permissions

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