Jump to content

Making a string into a variable


Nexus10

Recommended Posts

<?php $learn_textiles_lvl = 5; // original variable
$a = "$"."learn_".$industry."_lvl"; // attempt at reconstructing original variable when only $industry is known (it is not always 'textiles')
$current_level = $a; // test
echo("<br />Current level: $current_level"); ?>

 

How do I construct a variable and get its contents (existing) from a string function (or other way)? I.e. I have the varible $industry and need to get the contents of an existing variable called $learn_(INDUSTRY)_lvl where INDUSTRY is defined in the variable $industry.

 

Link to comment
https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/
Share on other sites

Try this:

<?php
$industry = 'textiles';
$learn_textiles_lvl = 5; // original variable
$a = ${'learn_' . $industry . '_lvl'}; // attempt at reconstructing original variable when only $industry is known (it is not always 'textiles')
$current_level = $a; // test
echo("<br />Current level: $current_level");
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.