Jump to content

Archived

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

Guest devans

converting a variable

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

Share this post


Link to post
Share on other sites
you can try the [url=http://us3.php.net/manual/en/function.eval.php]eval[/url] function

[code]
echo eval($some_info.";");
[/code]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.