Click here to get back home

dynamic variable names?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
dynamic variable names? DavidSeck.com 06-07-2008
Get Chitika Premium
Posted by DavidSeck.com on June 7, 2008, 4:40 pm
Please log in for more thread options
Hi guys,

first post :)

my question: is it possible to have dynamic variable names, I mean
something like this:

for($i=0;$i<x;$i++){
$y_$i = blabla;
}

in other words I want the number of the loop to be displayer in the
name of the variable.

Thanks for help.

Posted by Iván Sánchez on June 7, 2008, 5:27 pm
Please log in for more thread options
DavidSeck.com@gmail.com wrote:

> my question: is it possible to have dynamic variable names, I mean
> something like this:

They're called "variable variables" and there is a whole section about them
in the PHP manual.

> for($i=0;$i<x;$i++){
> $y_$i = blabla;
> }
>
> in other words I want the number of the loop to be displayer in the
> name of the variable.

In this case, you'll be better off using arrays.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Proudly running Debian Linux with 2.6.24-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-1 generating this signature.
Uptime: 23:26:41 up 15 days, 10:58, 4 users, load average: 0.81, 0.60,
0.48


Posted by The Hajj on June 8, 2008, 9:57 am
Please log in for more thread options
On Jun 7, 4:40 pm, DavidSeck....@gmail.com wrote:
> Hi guys,
>
> first post :)
>
> my question: is it possible to have dynamic variable names, I mean
> something like this:
>
> for($i=0;$i<x;$i++){
> $y_$i = blabla;
>
> }
>
> in other words I want the number of the loop to be displayer in the
> name of the variable.
>
> Thanks for help.

Not sure why but you can do it one of two ways:
<code>
<? php


for ($i=0; $i < 10; $i++) {

if ($i == 0) {
$$i = "This is the first loop.";
$y_ = "This is the first loop.";
}

if ($i == 1) {
$$i = "This is the second loop.";
$y_ = "This is the second loop.";
}

if ($i == 2) {
$$i = "This is the third loop.";
$y_ = "This is the third loop.";
}

if ($i == 3) {
$$i = "This is the fourth loop.";
$y_ = "This is the fourth loop.";
}

if ($i == 4) {
$$i = "This is the fifth loop.";
$y_ = "This is the fifth loop.";
}

if ($i == 5) {
$$i = "This is the sixth loop.";
$y_ = "This is the sixth loop.";
}

if ($i == 6) {
$$i = "This is the seventh loop.";
$y_ = "This is the seventh loop.";
}

if ($i == 7) {
$$i = "This is the eighth loop.";
$y_ = "This is the eighth loop.";
}

if ($i == 8) {
$$i = "This is the nineth loop.";
$y_ = "This is the nineth loop.";
}

if ($i == 9) {
$$i = "This is the tenth loop.";
$y_ = "This is the tenth loop.";
}

}

$x = 0;
while ($x < 10) {
echo $$x . "<br />";
echo $y_ . "<br />";
$x++;
}

?>
</code>

Posted by The Hajj on June 8, 2008, 10:39 am
Please log in for more thread options
** correction **
Not sure why but you would want to do it but you could do it one of
two ways:

Posted by AnrDaemon on June 8, 2008, 12:22 pm
Please log in for more thread options
Greetings, DavidSeck.com@gmail.com.
In reply to Your message dated Sunday, June 8, 2008, 00:40:28,

> first post :)

And epic fail...

> my question: is it possible to have dynamic variable names, I mean
> something like this:

> for($i=0;$i<x;$i++){
> $y_$i = blabla;
> }

> in other words I want the number of the loop to be displayer in the
> name of the variable.

It is possible, read about Variable Variables
http://www.php.net/manual/en/language.variables.variable.php
but why the hell you want it that way?
Use arrays instead.

<?php

for($i = 0; $i < x; $i++)
{
$y[$i] = blabla;
}

?>

Much clearer and impose no problem for later work with that variable.

P.S.
Use spaces in your code. It makes code at least more readable, if not
understandable.


--


Similar ThreadsPosted
Re: dynamic variable names for objects April 3, 2007, 6:54 pm
Re: dynamic variable names for objects April 4, 2007, 10:04 am
dynamic variable names for objects April 3, 2007, 9:40 am
Cannot use "-" in variable names? December 9, 2005, 2:20 pm
function and variable names January 24, 2005, 5:02 am
How can I get session variable names May 6, 2007, 2:31 pm
Getting variable names passed to form in php October 14, 2007, 7:13 am
Correct syntax for table variable names containing a space November 22, 2004, 8:50 am
dynamic variable name March 5, 2006, 8:29 am
Dynamic Variable Name $$var March 14, 2006, 5:30 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap