Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2012, 11:27 AM   PM User | #1
Sloth
New to the CF scene

 
Join Date: Jul 2012
Location: Manchester
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Sloth is an unknown quantity at this point
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
Sloth is offline   Reply With Quote
Old 08-10-2012, 12:23 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.



.
mlseim is offline   Reply With Quote
Old 08-10-2012, 02:20 PM   PM User | #3
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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..
Arcticwarrio is offline   Reply With Quote
Old 08-10-2012, 02:37 PM   PM User | #4
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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..
Arcticwarrio is offline   Reply With Quote
Old 08-10-2012, 03:08 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.



.
mlseim is offline   Reply With Quote
Old 08-10-2012, 03:29 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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(1count($aKeys));
$aResult array_combine($aKeys$aValues); 
Would generate an associative array of the letters with the numbers.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Arcticwarrio (08-10-2012)
Old 08-10-2012, 04:19 PM   PM User | #7
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 585
Thanks: 15
Thanked 65 Times in 65 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 PM.


Advertisement
Log in to turn off these ads.