SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Dec 18, 2006, 09:45 #1
- 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.
-
Dec 18, 2006, 10:10 #2
why u didn't use arrays?
-
Dec 18, 2006, 10:43 #3
- 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>';
}
?>
It is really better to store these strings in an array than to use variables like that.
-
Dec 18, 2006, 10:47 #4
- 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
-
Dec 18, 2006, 10:52 #5
- 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
}
?>
-
Dec 18, 2006, 11:10 #6
- 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[]">
-
Dec 18, 2006, 11:47 #7
- Join Date
- Mar 2006
- Posts
- 6,132
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
more specifically
http://www.php.net/manual/en/faq.htm...aq.html.arrays
-
Dec 18, 2006, 11:58 #8
- 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();
?>
-
Dec 18, 2006, 12:30 #9
- 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=1 ; $i<=2 ; $i++)
{
$varname = 'helpMe'.$i;
echo $this->$varname ;
}
}
}
$aa = new help();
?>
Bookmarks