Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can PHP output to variables?

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I want to be able to use implode to take the elements in an array and assign them to individual variables. Is this possible? I've done similar things in other languages, but haven't been able to figure this one out yet.

Code:
//$skills = array('sql', 'perl', 'php', 'html');
$tagline = implode(' ', $skills);

for($i = -1; $i < count($skills); $i++)
{
   echo "skill #$i = $skills[$i] <br />";
}
echo "List of Skills: $tagline";

The output looks like this:
Code:
skill #0 = sql
skill #1 = perl
skill #2 = php
skill #3 = html
List of Skills: sql perl php html shell perl jcl cobol

I'd like to be able to output the array contents to variables like skill1, skill2, etc.

Thanks!
Peter V.
 
list() should help too if you know the number of items.

but probably easiest is extract()

Code:
$skills = array('sql', 'perl', 'php', 'html');
extract($skills, EXTR_PREFIX_ALL, 'skills');



 
jpadie, in the case I'm working on, the number of items is volatile, so list wouldn't work.

ingresman, I'm trying your idea, and it's giving me the variable name I'm looking for, but I haven't yet been able to assign the variable to it. All I get when I echo it is the new variable name.
 
if the number is volatile then extract() is what you need as per my previous post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top