What's opposite of eval function? - php

I wonder how I can use the exact opposite of the eval function.
This is my code:
$test = 1;
$t = '$test';
echo opposite_eval($t);
I have to 1 output from above codes, how can i use method, function or class ?
Thanks for your estemeed help friends !

i cheated a little by removing the $ from the $t string (you can do that in the function its just a string:
$t = 'test';
function opposite_eval($t){
$test = 1;
return($$t);
}
echo opposite_eval($t); //=1
the phase you want to look in to is variable variables

I think you want a variables variable
In your case it would be:
$test = 1;
$t = 'test';
echo $$t;
// output: 1
Addon:
You could also do things like that:
$test['x'] = 1;
$t = 'test';
echo $$t['x'];
Whereas this will not work:
$test['x'] = 1;
$t = "test['x']";
echo $$t;
// Produces: NOTICE Undefined variable: test['x'] on line number 6
neither will:
$test = new stdClass();
$test->x = 1;
$t = "test->x";
echo $$t;
but this will work:
$test = new stdClass();
$test->x = 1;
$t = "{$test->x}";
echo $t;
and this will also work:
$test =[];
$test['x'] = 1;
$t = "{$test['x']}";
echo $t;

Related

Call an existing php variable by concatenating two strings

I have these seven php variables:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
results = array('A','B','C');
I would like to loop through the results array and pass the corresponding variables to a function.
foreach($results as $value) {
$data = '$data'.$value;
$target = '$target'.$value;
buildOutput('$data'.$value,'$target'.$value);
}
// example, first time thru, wish to pass $dataA variable and $targetA variable
function buildOutput($data,$target) {
echo "data=$data,target=$target<br>"; // echo's strings "$dataA" and "$targetA"
}
I cannot figure out how to declare the variables. Or if this is even possible.
I have more than just $data and $target variables, but I simplified down to two for the question.
I would not recommend using it, but try this:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
$results = array('A','B','C');
foreach($results as $value) {
buildOutput(${'data'.$value}, ${'target'.$value});
}
function buildOutput($data, $target) {
echo "data=$data,target=$target<br>";
}

PHP Loop Dynamic Variable

I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:
$ct = 1;
foreach ($record as $rec){
$var.$ct = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?
You're looking for variable variables.
Create the variable name as a string, and then assign it:
$ct = 1;
foreach( $record as $rec )
{
$name = 'var'.$ct;
$$name = $rec['Name'];
$ct++;
}
echo $var1;
It would be much better to create an array, though:
$names = [ ];
foreach( $record as $rec )
{
$names[] = $rec['Name'];
}
echo $names[0];
You can use different syntax with {}
$ct = 1;
foreach ($record as $rec){
${'var' . $ct++} = $rec['Name'];
}
echo $var1;
Although isn't it better just to use an array?
Working fiddle
You can with a double $.
$var = "variable";
$$var = "test";
echo $variable;
//echoes "test"
in your example:
$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
$$varname = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
Please try this, let me know if it works for you.
I use a prefix with the dynamic variable.
$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
$temp = $ct;
$ct = $prefix.$ct;
$$ct = $rec;
$ct = $temp + 1;
}
echo $var1;
You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.
In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.
$var = [];
foreach( $record as $k => $rec )
{
$var['var'.$k] = $rec['Name'];
}
extract($var);
echo $var0; //or $var_1 or $var_2

php could not use $$ with array

I know that
$b = 1;
$var = "b";
$$var = 2;
echo $b;
will show 2
But when I try it on array, it fail
$c[1] = 1;
$var = "c[1]";
$$var = 2;
echo $c[1];
$d[1] = 1;
$var = "d";
$$var[1] = 2;
echo $d[1];
they both show 1, why?
In your first example, you can't use the index because it is assumed part of the variable name.
In the second, you need to use the curly braces for a complex syntax to disambiguate. This way PHP knows that it's the contents of $d[1] and not the contents of $var[1].
$d[1] = 1;
$var = "d";
${$var}[1] = 2;
echo $d[1];

Store function to variable in Php

I'd like to store a function in a variable, is that possible?
Let's see this example
<?php
$tot_prezzo = 200;
$x = 75;
$y = 25;
function addition() {
echo $GLOBALS['imponibile'] = $GLOBALS['tot_prezzo'] + $GLOBALS['x'];
}
$func = 'addition';
$func();
?>
How can I store "$func()" in a variable?
I've tried with -> $func_var = $func(), but it doesn't work.
What am I missing here?
Thank you!
This is called "Lambda Functions" and can be used like this:
$addition = function($a, $b) {
return $a+$b;
};
echo $addition(1,2); //3
In order to use a value from the function you simply have to return it.
$tot_prezzo = 200;
$x = 75;
$y = 25;
function addition($tot_prezzo,$x,$y) {
$imponibile = $tot_prezzo + $x;
return $imponibile;
}
$result = addition($tot_prezzo,$x,$y);
print $result;
Note the use of arguments instead of $GLOBALS also you are not using the $y variable.

php - use variable variable in while loop

I'm a bit confused about variable variables.
What I like to do is print the value of $var1, $var2 and $var3:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo $var.$i;
$i++;
}
I know $var.$i; is not correct, but I think it shows what I would like to achieve; the while-loop should change it to $var1, $var2 and $var3;
I've tried the following:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
$current_var = 'var'.$i;
$current_var = $$current_var;
echo $current_var;
$i++;
}
But that doesn't work. I think because $var1, $var2 and $var3 are recreated in the while-loop instead of using the actual value. Not sure if that's correct, but that the only thing I can think of.
Try this instead:
echo ${"var".$i};
Curly braces can resolve to variable names without having to use the dollar-dollar approach.
See: Variable Variables in PHP
Try this one.
<?php
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo ${'var'.$i};
$i++;
}
?>
Are you trying to do something like this. Then use array
$my_data = array();
$my_data[1] = 'a';
$my_data[2] = 'b';
$my_data[3] = 'c';
// Method 1
$i = 1;
while ($i <= 3) {
echo $my_data[$i];
$i++;
}
// Method 2
foreach ($my_data as $data) {
echo $data;
}
// Output
abc
abc

Categories