Jump to content

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
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.