Closed Thread Icon

Topic awaiting preservation: PHP - eval() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12427" title="Pages that link to Topic awaiting preservation: PHP - eval() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PHP - eval() <span class="small">(Page 1 of 1)</span>\

 
maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 09-17-2002 07:08

ok, this is really bugging me. I'm not even sure if this is the right function I want to use. So basically what I want to due is have the value of one variable be the name of another. Sort of hard to explain but here's a simplified example:

$var1 = "Some dumb Text";
$var2 = "var1";
somemagicalfunctionwhichmightbeeval(\$$var2); # or something like that

see what I mean? so like it returns the value of var1. Anybody understanding me?

behmer

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-17-2002 07:21

Mmmm. This is called "indirection" - turning data (a string) into code (a variable name). eval does it in Javascript, as well as the [] operators, so it would be logical if it did it in PHP also.

Try

eval("\$" . $var2)

I'm a complete beginner at PHP, but I know that it's similar to Perl, so that's the best guess I can make.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 09-17-2002 08:48

In PHP this can be done without using eval()...

<?php

$var1 = "Some dumb Text";
$var2 = "var1";

print(${$var2});

?>


Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 09-17-2002 13:32

what max describes is kown as variable variables. just have a look into the manual over at www.php.net/manual

« BackwardsOnwards »

Show Forum Drop Down Menu