473,555 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3596

"Owen Funkhouser" <fu**@highwaay. net> wrote in message
news:3F******** *******@highwaa y.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******** *******@highwaa y.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.c om> 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
3043
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 16, B-3010 Leuven, Belgium Bored with EDA the way it is? Check this: http://jandecaluwe.com/Tools/MyHDL/Overview.html
51
3125
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
16115
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 to pass both as "void *buf" so that it can accept any data type. But since I access the buffer and try to assign its elements to another I get...
11
1960
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 follows: portb = 0x0f; But when I want to define a register as follows, the code does not
16
2331
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 cannot be casted to a textbox. The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.
4
5223
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 = find_task_by_id(getpid()); 7. printf("%s",my->comm); error: dereferencing pointer to incomplete type
3
29680
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 -O3. I don't see any reason why optimization should change things much in this piece of code - there's no way to optimize it out and I have...
1
1905
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 strict-aliasing rules if "something" is of some other type, say "int".
12
3922
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
0
7621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7824
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8060
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7903
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5452
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5170
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.