SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo numbered $variable1, $variable2

    Hello let's go straight to my question, how can i echo variables named with same characters but different in number at the end.

    $helpMe1 = "Maybe this is easy for you but" ;
    $helpMe2 = "I just don't know how" ;

    and so on...goes until

    $helpMe999 = "I am not trying to be funny but Thank's in advance" ;

    so if I want to print out all the string in that 999 variables of course i don't want to write down the 999 variables manually but instead can I print using FOR Loop ? or any other way ? I have try to append number at the end of the variable but did not get the correct syntax. Thank's if you can help me out.

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why u didn't use arrays?

  3. #3
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $helpMe1 
    "Maybe this is easy for you but" ;
    $helpMe2 "I just don't know how" ;
    $helpMe3 "The point is to use variable variables" ;
    $helpMe4 "or store the strings in an array (preferable) instead of separate variables";

    for(
    $i=1;$i<=4;$i++)
    {
     
    $varname 'helpMe'.$i;
     echo $
    $varname.'<br>';
    }
    ?>
    Please also look at Variable Variables.

    It is really better to store these strings in an array than to use variables like that.

  4. #4
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Austin
    Posts
    401
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I highly recommend figuring out how to switch your variables to an array. The dynamic variable solution that SKDevelopment posted works fine, but it can get really confusing, really quickly, and a lot of people don't understand what to expect the output to be.
    Merchant Equipment Store - Merchant Services, POS, Equipment, and supplies.
    Merchant Account Blog | Ecommerce Blog

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know all about array as like below. However my real situation is, i want to get data from FORM submission, let's say upload field where i have javascript function that can add more upload field until infinity dynamically,hence the name of the upload field is assigned by the javascript with only the difference of the number at the end of the name, such as
    <input type=file name=helpMe1>
    <input type=file name=helpMe2>
    <?php
    $helpMe[1] = "text string 1";
    $helpMe[2] = "text string 2";
    $helpMe[3] = "text string 3";

    foreach( $helpMe as $arraykey => $arrayvalue){
    echo "String number: $arraykey, String text: $arrayvalue <br />";
    }
    ?>


    BTW thank's for your reply i have found the solution already like below :

    <?php
    $helpMe1 = 'string 1';
    $helpMe2 = 'string 2';
    $helpMe3 = 'string 3';

    for ($i=1; $i<=3 ; $i++){
    echo ${'helpMe'.$i}.'<br>'; // put brace around the name of the variable

    }
    ?>

  6. #6
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI: Please look at Uploading multiple files in the PHP Manual.

    Also, in your case you could use:
    PHP Code:
    <input type="file" name="helpMe[]">
    <
    input type="file" name="helpMe[]"
    to get the result as an array.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank's , that info teach me the new thing that we can put array in the name field. It is a big deal for me.....

    However for the variable variables, it does not work for the global variable, i try the following but did not work, the error is
    Notice: Undefined variable: this->helpMe1
    Notice: Undefined variable: this->helpMe2
    <?php

    class help{



    function help(){
    $this->helpMe1 = 'string 1' ;
    $this->helpMe2 = 'string 2' ;
    $this->helpYouEcho();
    }

    function helpYouEcho(){
    for($i=1 ; $i<=2 ; $i++){
    echo ${'this->helpMe'.$i} ;
    }
    }

    }

    $aa = new help();
    ?>

  9. #9
    SitePoint Addict
    Join Date
    Dec 2004
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php
    class help
    {
     function 
    help()
     {
      
    $this->helpMe1 'string 1' ;
      
    $this->helpMe2 'string 2' ;
      
    $this->helpYouEcho();
     }

     function 
    helpYouEcho()
     {
      for(
    $i=$i<=$i++)
      {
       
    $varname 'helpMe'.$i;
       echo 
    $this->$varname ;
      }
     }
    }

    $aa = new help();
    ?>


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
  •