Using a variable within a variable.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Carnadyne
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 4:37 pm

Using a variable within a variable.

Post 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]
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$j{$i . 'company'}
And stryks is right. An array would be much better.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Carnadyne
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 4:37 pm

Post by Carnadyne »

Thanks guys :)
Post Reply