generating variables on the fly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

generating variables on the fly

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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 :P
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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 :P
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), ...........
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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/>';
}
(#10850)
anielsud
Forum Newbie
Posts: 1
Joined: Wed Jan 03, 2007 2:39 am

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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
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'];
?>
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

OK i've Got My Answer
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sure you can.

Code: Select all

function d($d)
{
  $GLOBALS[$d] = $d;
}
The question is, why would you?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Thanks
feyd wrote:The question is, why would you?
:oops: To Hug php While Playing With it :lol:
Post Reply