Jump to content

converting a variable


Guest devans

Recommended Posts

Guest devans
This is probably an easy one, but great if someone could suggest a solution =)

I'm including various language files, depending on which is selected.  For any language, I define them such:
[code]
<?php
// english.php
// included english file
DEFINE('_USER_ID','User ID');
// blah blah a bunch more...
?>
[/code]

All is wonderful, but then I got the great idea of making my db names match.  The question is, how do I convert a string to a variable (if that makes any sense)?  For example:

[code]
<?php
// include above english file
include("english.php");

// do a query
$sql = 'SELECT foo FROM bar';
$result = mysql_query($sql) or die (mysql_errno().": select ".mysql_error()."<BR>" . $sql);
while ($row = mysql_fetch_array($result)) {
$some_info = $row['foo'];
}
// $some_info = _USER_ID
echo $some_info;  // this shows _USER_ID, not User ID
?>
[/code]

Is there any way of making the $some_info var echo the defined definition (i.e., in this case, User ID)?

daniel
Link to comment
https://forums.phpfreaks.com/topic/13344-converting-a-variable/
Share on other sites

Thanks nogray, you set me on the right track!  For any others having the same issue, this was the final solution that worked for me...

[code]
<?php
define("TEST_VAL","test value");
$test = "TEST_VAL";
echo $test . "<br/>"; // echos TEST_VAL
eval("\$test = $test;");
echo $test . "<br/>"; // echos test value
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/13344-converting-a-variable/#findComment-52936
Share on other sites

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.