473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dynamic variable names?

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.
Jun 27 '08 #1
7 2394
Da***********@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

Jun 27 '08 #2
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_{$i} = "This is the first loop.";
}

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

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

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

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

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

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

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

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

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

}

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

?>
</code>
Jun 27 '08 #3
** correction **
Not sure why but you would want to do it but you could do it one of
two ways:
Jun 27 '08 #4
Greetings, Da***********@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/languag...s.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.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 27 '08 #5
On Jun 8, 12:22 pm, AnrDaemon <anrdae...@freemail.ruwrote:
Greetings, DavidSeck....@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 Variableshttp://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.

--
Sincerely Yours, AnrDaemon <anrdae...@freemail.ru>
That would confuse me at first glance, would think it was an array,
but I never thought of doing it like that. #376 of millions of ways to
skin a cat :)
Jun 27 '08 #6
The Hajj wrote:
On Jun 8, 12:22 pm, AnrDaemon <anrdae...@freemail.ruwrote:
>Greetings, DavidSeck....@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 Variableshttp://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.

--
Sincerely Yours, AnrDaemon <anrdae...@freemail.ru>

That would confuse me at first glance, would think it was an array,
but I never thought of doing it like that. #376 of millions of ways to
skin a cat :)
It is an array. That's the whole point.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 27 '08 #7
On Jun 8, 9:54 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
The Hajj wrote:
On Jun 8, 12:22 pm, AnrDaemon <anrdae...@freemail.ruwrote:
Greetings, DavidSeck....@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 Variableshttp://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.
--
Sincerely Yours, AnrDaemon <anrdae...@freemail.ru>
That would confuse me at first glance, would think it was an array,
but I never thought of doing it like that. #376 of millions of ways to
skin a cat :)

It is an array. That's the whole point.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Doh!! auto type at runtime
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jas | last post by:
Hi All I guess I should just have tried this to see if it works but I don't have access to a machine running IIS and can't test it and I am itching for an answer. I want to use dynamic...
9
by: firegun9 | last post by:
Hello all, After looking for the solution for a while in the group, I know this is not possible in C++. So I just say my problme here. The input file is: dev1 1 1 dev2 0 0 0 6 There is a...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
5
by: Rolf Mander | last post by:
Hi, I need to use dynamic variable names but for objects. As you know something like that works fine: $variable="content"; $part="able"; echo ${"vari".$part}; // gives out content but i...
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.