Page 1 of 1

Using a variable within a variable.

Posted: Thu Oct 04, 2007 4:41 pm
by Carnadyne
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Rather simple problem...

Code: Select all

$j1company = "Company1";
$j2company = "Company2";

$num_jobs = 2;

for($i=1; $i<$num_jobs+1; $i++) {
	if ($j{$i}company != "") {
		echo $j{$i}company;
	}
}
Just wondering how I get the $i to register in the variable name?

Thanks!

Chris


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 04, 2007 5:31 pm
by Stryks
It depends on your situation of course, but where possible, I would suggest rewriting your code to something like the following.

Code: Select all

$company[1] = "Company 1";
$company[2] = "Company 2";

foreach($company as $item) echo $item;
I cant answer your question directly though, as I have honestly never tried to do it.

Hope this helps.

Posted: Thu Oct 04, 2007 6:06 pm
by s.dot

Code: Select all

$j{$i . 'company'}
And stryks is right. An array would be much better.

Posted: Thu Oct 04, 2007 8:25 pm
by Carnadyne
Thanks guys :)