Page 1 of 1
generating variables on the fly
Posted: Tue Jan 02, 2007 11:42 pm
by neel_basu
Can I generate variables in the fly
Such As Let $get_var = "hello";
And Now I Wanna Create A Variable on The fly named hello ( i mean the variable name of one variable would be the value of another variable )
And I wanna do it without help of any db or include or any other external files
Posted: Wed Jan 03, 2007 12:14 am
by hawleyjr
Posted: Wed Jan 03, 2007 1:12 am
by dude81
say your example is like this
Code: Select all
for($i=0;$i<10;$i++){
$x=$i;
//Creating variable on fly shall be possible by the way below simply by saying $ of $x
$y=$$x;
}
Dude 81

Posted: Wed Jan 03, 2007 1:24 am
by neel_basu
dude81 wrote:say your example is like this
Code: Select all
for($i=0;$i<10;$i++){
$x=$i;
//Creating variable on fly shall be possible by the way below simply by saying $ of $x
$y=$$x;
}
Dude 81

Ya I did this
=========
Code: Select all
<?php
for($i=0;$i<=10;$i++)
{
$x=$i;
//Creating variable on fly shall be possible by the way below simply by saying $ of $x
$y=$$x;
echo "\$y(".$y.")-\$x(".$x.")-\$i(".$i.")<br />\n";
}
?>
It made an output
=============
php output wrote:$y()-$x(0)-$i(0)
$y()-$x(1)-$i(1)
$y()-$x(2)-$i(2)
$y()-$x(3)-$i(3)
$y()-$x(4)-$i(4)
$y()-$x(5)-$i(5)
$y()-$x(6)-$i(6)
$y()-$x(7)-$i(7)
$y()-$x(8)-$i(8)
$y()-$x(9)-$i(9)
Look $y() Its Simply Blank But I think I would be like this $y(0), $y(1), ...........
Posted: Wed Jan 03, 2007 1:35 am
by Christopher
Shouldn't this be something like this because var names must start with a letter or underscore:
Code: Select all
for($i=0; $i<10; $i++){
$x = "myvar$i";
$$x = "$x is $i";
}
for($i=0; $i<10; $i++){
$x = "myvar$i";
echo $$x . '<br/>';
}
Posted: Wed Jan 03, 2007 10:09 am
by anielsud
Actually, when dynamically creating vars they can also be just straight numbers. The problem with the original code was that $$x was unset when you assigned $y to be equal to it.
Code: Select all
<?php
$x=0;
//Creating variable on fly shall be possible by the way below simply by saying $ of $x
$$x="Variable $0 is equal to this string";
?>
So to correct your original code:
Code: Select all
<?php
for($i=0;$i<=10;$i++)
{
$x=$i;
//Creating variable on fly shall be possible by the way below simply by saying $ of $x
$$x=$x;
$y=$$x;
echo "\$y(".$y.")-\$x(".$x.")-\$i(".$i.")<br />\n";
}
?>
now $i, $x, $$x, and $y are all equal. The system name for $$x is actually going to be $0 through $10.
Posted: Wed Jan 03, 2007 10:35 am
by neel_basu
I think I didn't make you understand what i was trying to do
Suppose
I have A php Script in which the code is
Code: Select all
<?php
$get_var_name = $_GET['var_name'];
// Now I Wanna Make A Variable Whats name is the value of the $get_var_name
//Suppose $get_var_name 's value is "hi"
//Now It will Make A Variable Dinamically named $hi
?>
I am Explaining More Clearly
Suppose I Saved The page as get_var_name.php Which has the previously stated codes in it
now i go to
http://localhost/get_var_name.php?var_name=hello
it would make A variable $hello instantly in runtime
Posted: Wed Jan 03, 2007 10:38 am
by RobertGonzalez
Hawley pointed this answer out already.
Code: Select all
<?php
// This would yield $hello = 'hello' if $_GET['var_name'] is set
${$_GET['var_name']} = $_GET['var_name'];
?>
Posted: Wed Jan 03, 2007 12:00 pm
by neel_basu
I mean are there any function in php that would work like
Code: Select all
create_var($new_variable_name, $value);
That Would make a global variable
Posted: Wed Jan 03, 2007 12:02 pm
by Begby
Code: Select all
$var = 'finland' ;
// This creates $finland
$$var = 'norway' ;
echo $finland ; // prints norway
If your code depends on this, stop, go back and rethink it.
Posted: Wed Jan 03, 2007 12:19 pm
by neel_basu
OK i've Got My Answer
Thanks
Posted: Wed Jan 03, 2007 12:22 pm
by RobertGonzalez
neel_basu wrote:I mean are there any function in php that would work like
Code: Select all
create_var($new_variable_name, $value);
That Would make a global variable
I am not sure that you can make a variable variable global all in one shot.
Posted: Wed Jan 03, 2007 12:31 pm
by neel_basu
Everah wrote:neel_basu wrote:I mean are there any function in php that would work like
Code: Select all
create_var($new_variable_name, $value);
That Would make a global variable
I am not sure that you can make a variable variable global all in one shot.
Yes I Was Trying To get That Type Answer ( Wheather Its Possible or not The answer would be y/n ). Thank you very much as you understood what my question was.
I mean are there any functions for it ? or not.
if not i have to create one by own.
Posted: Wed Jan 03, 2007 12:32 pm
by feyd
Sure you can.
Code: Select all
function d($d)
{
$GLOBALS[$d] = $d;
}
The question is, why would you?
Posted: Wed Jan 03, 2007 12:40 pm
by neel_basu
Thanks
feyd wrote:The question is, why would you?

To Hug php While Playing With it
