Enjoy an ad free experience by logging in. Not a member yet?
Register .
08-10-2012, 11:27 AM
PM User |
#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
08-10-2012, 12:23 PM
PM User |
#2
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 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.
.
08-10-2012, 02:20 PM
PM User |
#3
Regular Coder
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 Posts
edit.
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Last edited by Arcticwarrio; 08-10-2012 at 02:41 PM ..
08-10-2012, 02:37 PM
PM User |
#4
Regular Coder
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 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 ; ?>
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Last edited by Arcticwarrio; 08-10-2012 at 02:40 PM ..
08-10-2012, 03:08 PM
PM User |
#5
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 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.
.
08-10-2012, 03:29 PM
PM User |
#6
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 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 ( 1 , count ( $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:
08-10-2012, 04:19 PM
PM User |
#7
Regular Coder
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 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.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 03:33 PM .
Advertisement
Log in to turn off these ads.