Get a variable name from database - php

I'm a little confused - how do I get a variable name stored to a database?!
Record in the database is a string: "$test"
The variable $user is set before the records a fetched from database. So I want to "convert" this string to a real variable to get the value of it.
The following didn't work:
// $test is set to 'bla'
$test = 'bla';
// $var is the value from the database
$var = '$test';
// print $test
echo ${$var};
I know that it would work if I remove the '$' from the database record
$var = 'test';
echo $$var;
But how to handle this without?

String replace in better option, if you don't want to replace $ you can use eval function:
$test = 'bla';
$var = '$test';
eval("\$var = \"$var\";");
echo $var; //output: bla

I was facing a similar problem just now.
Here is what I did:
$res[0] is obtained from mysql_fetch_array() and it contains another query which has $variable embedded.
$qry="select query from sql_list where id=".$sql_id;
$result=mysql_query($qry);
$res=mysql_fetch_array($result);
eval("\$qry_2 = \"$res[0]\";");
mysql_query($qry_2)
It works! Maybe someone can suggest a better way.

This is 3 years later but since no one give you your answer, maybe this will help you or the next person searching for this.
From what I understand, you already declared a variable and stored the name of that var in the db.
$var = '$test'; // from db
$var[0] = ''; // remove first letter, simple & much faster than ( trim or substr )
//content of var
$var_content = ${$var};

Related

Create a property of object with the value of variable [duplicate]

How do i dynamically assign a name to a php object?
for example how would i assign a object to a var that is the id of the db row that i am using to create objects.
for example
$<idnum>= new object();
where idnum is the id from my database.
You can use the a double dollar sign to create a variable with the name of the value of another one for example:
$idnum = "myVar";
$$idnum = new object(); // This is equivalent to $myVar = new object();
But make sure if you really need to do that, your code can get really messy if you don't have enough care or you abuse of using this "feature"...
I think you can better use arrays or hash tables rather than polluting the global namespace with dynamically created variables.
this little snippet works for me
$num=500;
${"id$num"} = 1234;
echo $id500;
basically just use the curly brackets to surround your variable name and prepend a $;
You can do something like this:
${"test123"} = "hello";
echo $test123; //will echo "hello"
$foo = "mystring";
${$foo} = "a value";
echo $mystring; //will echo "a value";
http://us2.php.net/language.variables.variable

Add two $row together in one php echo

I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.) for this, not the plus sign (+).
If the value of $row_ColNumc['ColNaumNo'] is 32 then the value stored in variable $name is 'T32R';
You can then prepend the variable $name with an extra $ to use it as the name of another variable (indirection). The code echo($$name); prints the content of variable $T32R (if any).
If the variable $T32R stores an array then the syntax $$name[0] is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
You can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);

creating variable name by concatenating strings in php

I have a file named questions.php with an array as follows :
$question12 = array("Which is the tallest mountain","Mt Everest");
I am including this file in another file as follows :
require_once('questions.php');
$var = 12;
$question = '$question'.$var.'[0]';
echo $question;
The above code just outputs the following string (not the contents of the variable):
$question12[0]
But I want the variable $question to contain the string present in $question12[0].
How do I accomplish this?
Variable variable is not recommended, but the answer is below:
$question = ${'question'.$var}[0];
You're looking for variable variables.
$id = 12;
$q = "question{$id}";
$q = $$q[0];
You should seriously consider looking into multidimensional arrays to stop having multiple arrays.
Just use $question12[0]. It will give you the desired output.
Using the $var you can do it like this:-
$question = ${'question'. $var}[index];
Sorry, im going to get some hate for mentioning something evil but still it is one of the options
<?php
$question12 = array("Which is the tallest mountain","Mt Everest");
$var = 12;
$question = '$question'.$var.'[0]';
eval("echo $question;");
?>
P.S: eval() is that evil

Using array value with index as Variable Variable

The title may be a little confusing. This is my problem:
I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:
$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;
That would print: "hello". Now, I've got a problem with this:
$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;
That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?
Thanks in advance.
For the moment, I could only think of something like this:
<?php
// literal are simple
$literal = "Hello";
$vv = "literal";
echo $$vv . "\n";
// prints "Hello"
// for containers it's not so simple anymore
$container = array("Hello" => "World");
$vv = "container";
$reniatnoc = $$vv;
echo $reniatnoc["Hello"] . "\n";
// prints "World"
?>
The problem here is that (quoting from php: access array value on the fly):
the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.
Would PHP allow the subscript notation anywhere, one could write this more dense as
echo $$vv["Hello"]
Side note: I guess using variable variables isn't that sane to use in production.
How about this? (NOTE: variable variables are as bad as goto)
$variablename = 'array';
$key = 'index';
echo $$variablename[$key];

Dynamic Naming of PHP Objects

How do i dynamically assign a name to a php object?
for example how would i assign a object to a var that is the id of the db row that i am using to create objects.
for example
$<idnum>= new object();
where idnum is the id from my database.
You can use the a double dollar sign to create a variable with the name of the value of another one for example:
$idnum = "myVar";
$$idnum = new object(); // This is equivalent to $myVar = new object();
But make sure if you really need to do that, your code can get really messy if you don't have enough care or you abuse of using this "feature"...
I think you can better use arrays or hash tables rather than polluting the global namespace with dynamically created variables.
this little snippet works for me
$num=500;
${"id$num"} = 1234;
echo $id500;
basically just use the curly brackets to surround your variable name and prepend a $;
You can do something like this:
${"test123"} = "hello";
echo $test123; //will echo "hello"
$foo = "mystring";
${$foo} = "a value";
echo $mystring; //will echo "a value";
http://us2.php.net/language.variables.variable

Categories