Page 1 of 1

manipulating variables

Posted: Sat Jun 04, 2005 12:55 pm
by jaymoore_299
If I initialize variable values:
$some_variable1 = 0;
$some_variable2 = 0;
... etc

And if I only have the variable name as a string "some_variable1" received as input from a different file, how do I then modify that variable without directly referring to that name by using the following:
$some_variable1++;
$some_variable1 = ... ;

I'm trying to modify variables based on the text strings I receive from a file. I don't want to make options for all possible variable names called because I want to receive any kind of variable name and if it doesn't exist, I want it to be created, then modified accordingly.

I'm basically trying to do something like this.
$some_variable1 = 0;
$string = "some_variable1"
"$some_variable1" = "$some_variable1" + 1;
print $some_variable; // outputs 1

though this is obviously incorrect code.

Posted: Sat Jun 04, 2005 1:03 pm
by neophyte
Sounds like you need variable variables. Read this and see if it helps:

http://us4.php.net/language.variables.variable

Posted: Sat Jun 04, 2005 1:07 pm
by anjanesh
COOL !!! Didnt know abt this. Thanks neophyte.

Code: Select all

<?php
$some_variable1 = 5;
$string = &quote;some_variable1&quote;;
$$string++;
echo $some_variable1;
?>

Posted: Sat Jun 04, 2005 1:32 pm
by jaymoore_299
yes I believe that will do. Thanks