473,414 Members | 1,947 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,414 software developers and data experts.

Dereferencing variable name

Maybe someone can help me out here:

$name1 = "Owen";
$name2 = "Funkhouser";

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = $name$i -->\n";
}

I'd like to see:

<!-- name1 = Owen -->
<!-- name2 = Funkhouser -->

Instead, I get:

<!-- name1 = 1 -->
<!-- name2 = 2 -->

What am I doing wrong?
Jul 17 '05 #1
8 3585

"Owen Funkhouser" <fu**@highwaay.net> wrote in message
news:3F***************@highwaay.net...
Maybe someone can help me out here:

$name1 = "Owen";
$name2 = "Funkhouser";

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = $name$i -->\n";
}

I'd like to see:

<!-- name1 = Owen -->
<!-- name2 = Funkhouser -->

Instead, I get:

<!-- name1 = 1 -->
<!-- name2 = 2 -->

What am I doing wrong?

Dynamic variable names are done with the double dollar sign. Try this:

$name1 = "Owen";
$name2 = "Funkhouser";

for ($i = 1; $i < 3; $i++ ) {
$varName = "name{$i}";
echo "<!-- $varName = $$varName -->\n";
}
Jul 17 '05 #2
Phlarmf wrote:

"Owen Funkhouser" <fu**@highwaay.net> wrote in message
news:3F***************@highwaay.net...
Maybe someone can help me out here:

$name1 = "Owen";
$name2 = "Funkhouser";

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = $name$i -->\n";
}

I'd like to see:

<!-- name1 = Owen -->
<!-- name2 = Funkhouser -->

Instead, I get:

<!-- name1 = 1 -->
<!-- name2 = 2 -->

What am I doing wrong?


Dynamic variable names are done with the double dollar sign. Try this:

$name1 = "Owen";
$name2 = "Funkhouser";

for ($i = 1; $i < 3; $i++ ) {
$varName = "name{$i}";
echo "<!-- $varName = $$varName -->\n";
}


I really want to avoid having another variable around. But you got me going in the right
direction. The following seems to work well:

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = ${"name$i"} -->\n";
}
Jul 17 '05 #3
Hi Owen!
I really want to avoid having another variable around. But you got me going in the right
direction. The following seems to work well:

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = ${"name$i"} -->\n";
}


If you want to avoid unnecessary variables you might want to look into
arrays. See www.php.net

HTH,Jochen

--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #4
Jochen Daum wrote:

Hi Owen!
I really want to avoid having another variable around. But you got me going in the right
direction. The following seems to work well:

for ($i = 1; $i < 3; $i++ ) {
echo "<!-- name$i = ${"name$i"} -->\n";
}


If you want to avoid unnecessary variables you might want to look into
arrays. See www.php.net


The data actually ends up in an array; I elided that information. In a nutshell, I'm
converting input from a form's POST into an array.
Jul 17 '05 #5
Owen Funkhouser wrote on Wednesday 24 September 2003 09:29:
The data actually ends up in an array; I elided that information. In a
nutshell, I'm converting input from a form's POST into an array.


If you have control over the form being submitted, use an array when
assigning form element names, such as:

<input type="text" name="arr[0]" />
<input type="text" name="arr[1]" />
etc.

After the form is submitted, the resulting variable $_POST["arr"] will be an
array also.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 17 '05 #6
Zurab Davitiani wrote:

Owen Funkhouser wrote on Wednesday 24 September 2003 09:29:
The data actually ends up in an array; I elided that information. In a
nutshell, I'm converting input from a form's POST into an array.


If you have control over the form being submitted, use an array when
assigning form element names, such as:

<input type="text" name="arr[0]" />
<input type="text" name="arr[1]" />
etc.

After the form is submitted, the resulting variable $_POST["arr"] will be an
array also.


Thank you! This will remove many large chunks of code and reduce the size of the SDD
greatly.
Jul 17 '05 #7
I noticed that Message-ID:
<sk*****************@newssvr25.news.prodigy.com> from Zurab Davitiani
contained the following:
<input type="text" name="arr[0]" />
<input type="text" name="arr[1]" />
etc.

After the form is submitted, the resulting variable $_POST["arr"] will be an
array also.


So the values will be in $_POST["arr[0]"],$_POST["arr[1]"] and so on?

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #8
On Thu, 25 Sep 2003 00:09:52 +0100, Geoff Berrow wrote:
So the values will be in $_POST["arr[0]"],$_POST["arr[1]"] and so on?


Not quite, but very near :-)

The values will be in $_POST['arr'][0], $_POST['arr'][1], etc
and you can treat the array with the foreach construct:

<?php
echo '<table>';
foreach ($_POST['arr'] as $k=>$v) {
echo '<tr><td>', $k, '</td><td>', $v, '</td></tr>';
}
echo '</table>';
?>

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #9

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

Similar topics

8
by: Jan Decaluwe | last post by:
Is there a way to dereference a cell object (that is, get the object that it references to) in Python? Regards, Jan -- Jan Decaluwe - Resources bvba - http://jandecaluwe.com Losbergenlaan...
51
by: BigMan | last post by:
Does the C++ standard define what should happen in case of NULL pointer dereferencing. If not, does it say that it is illegal? Where, if so, does it say it?
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
11
by: jlara | last post by:
Working on an embedded controller, I generally find it convenient to define the following de-referenced pointer: #define portb (*(unsigned char *)(0x03u)) This enable me to write to a port as...
16
by: Michael Maes | last post by:
Hi, How would I handle Dereferencing in vb.Net 2003. Something like: Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox) This sadly won't work because a type of string...
4
by: Pritam | last post by:
line 7: error: dereferencing pointer to incomplete type 1. #include<stdio.h> 2. #include<sys/stat.h> 3. #include<stdlib.h> 4. void execname() { 5. struct task_struct *my; 6. my =...
3
by: David Mathog | last post by:
I have a program for which this line: if(! lstrtol(&atoken,length-2,(long *) &(lclparams->pad)) || (lclparams->pad< 0)){ generates the warning below, but ONLY if the gcc compiler is at -O2 or...
1
by: David Mathog | last post by:
We've established that a line of code like: (void) function( (long *) &(something) ); may (and probably should) generate one of these warning: dereferencing type-punned pointer will break...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.