generating variables on the fly
Moderator: General Moderators
- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
generating variables on the fly
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
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
say your example is like this
Dude 81 
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;
}- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
Ya I did thisdude81 wrote:say your example is like thisDude 81Code: 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; }
=========
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";
}
?>=============
Look $y() Its Simply Blank But I think I would be like this $y(0), $y(1), ...........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)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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/>';
}(#10850)
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.
So to correct your original code:
now $i, $x, $$x, and $y are all equal. The system name for $$x is actually going to be $0 through $10.
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";
?>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";
}
?>- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
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
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
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
?>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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Hawley pointed this answer out already.neel_basu wrote: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
hawleyjr wrote:http://us2.php.net/language.variables.variable
Code: Select all
<?php
// This would yield $hello = 'hello' if $_GET['var_name'] is set
${$_GET['var_name']} = $_GET['var_name'];
?>- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
I mean are there any function in php that would work like
That Would make a global variable
Code: Select all
create_var($new_variable_name, $value);Code: Select all
$var = 'finland' ;
// This creates $finland
$$var = 'norway' ;
echo $finland ; // prints norway- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I am not sure that you can make a variable variable global all in one shot.neel_basu wrote:I mean are there any function in php that would work likeThat Would make a global variableCode: Select all
create_var($new_variable_name, $value);
- neel_basu
- Forum Contributor
- Posts: 454
- Joined: Wed Dec 06, 2006 9:33 am
- Location: Picnic Garden, Kolkata, India
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.Everah wrote:I am not sure that you can make a variable variable global all in one shot.neel_basu wrote:I mean are there any function in php that would work likeThat Would make a global variableCode: Select all
create_var($new_variable_name, $value);
I mean are there any functions for it ? or not.
if not i have to create one by own.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Sure you can.The question is, why would you?
Code: Select all
function d($d)
{
$GLOBALS[$d] = $d;
}