473,671 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Variables not working right?

If I'm thinking right, this should work...can someone help me
understand why it's not.

//$loop = 1
$test = "num$loop"; //test should now equal "num1"
$ttest = $$test; //ttest should now reference $num1
echo $test; //this echo DOES show "num1"
echo ${$ttest}; //this echo shows nothing (blank)
echo $num1; //this echo shows the contents of $num1
correctly.

What I'm doing is this: From a previous form I have outputted a bunch
of variables: $num1, $num2, $num3...$name1, $name2, $name3.

In my script I want to put in a loop a dynamic reference to each of
these...the step the loop is in will = the last digit in the variable.

What am I missing here? Can someone help?

Thanks,
Chris
Jul 17 '05 #1
2 1855
Chris wrote:
If I'm thinking right, this should work...can someone help me
understand why it's not.

//$loop = 1
$test = "num$loop"; //test should now equal "num1"
$ttest = $$test; //ttest should now reference $num1
echo $test; //this echo DOES show "num1"
echo ${$ttest}; //this echo shows nothing (blank)
echo $num1; //this echo shows the contents of $num1
correctly.

What I'm doing is this: From a previous form I have outputted a bunch
of variables: $num1, $num2, $num3...$name1, $name2, $name3.

In my script I want to put in a loop a dynamic reference to each of
these...the step the loop is in will = the last digit in the variable.

What am I missing here? Can someone help?

Thanks,
Chris

$num1 = 'hello';
$loop = 1;
$test = "num$loop"; //test should now equal "num1"
$ttest = $$test; //ttest should now reference $num1
echo $test; //this echo DOES show "num1"
echo $ttest; //this echo shows "hello"
echo $num1"; //this echo shows the contents of $num1

This works as intended. Your error was in this line:
echo ${$ttest}; //this echo shows nothing (blank)


$ttest is already referencing another variable. You don't need to
reference it again. In essence, you were echoing $$num1, which, as you
know, is a reference to whatever $num1 is referencing, which is nothing.
In this example, $ttest = $num1 and not $$num1 (which is what you
were trying to echo).

Hope that was clear.

-Jay

Jul 17 '05 #2
> This works as intended. Your error was in this line:
> echo ${$ttest}; //this echo shows nothing (blank)


$ttest is already referencing another variable. You don't need to
reference it again. In essence, you were echoing $$num1, which, as you
know, is a reference to whatever $num1 is referencing, which is nothing.
In this example, $ttest = $num1 and not $$num1 (which is what you
were trying to echo).

Hope that was clear.

-Jay


AHH....thanks Jay. I see what you are saying and it does make sense! :-)

Thanks,
Chris
Jul 17 '05 #3

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

Similar topics

83
6481
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
10
2325
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example: pi = 3.14159... mynum = 3.14226... The mynum is accurate to 2 decimal places compared to pi. I could figure this out easy with a char array since I could just take a character one at a time from each array and compare it. However with a...
134
7861
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
5
3291
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
23
19171
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
18
4047
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
3
8009
by: PseudoMega | last post by:
I'm working with a PHP page I wrote which searches through records in a MySQL database. I have a <form method="post"which currently passes all of search variables into the session array. I'd like to change the form to GET instead of POST so that all of the variables are in the URL. My problem is that I have already been using pagination code which creates links like <a href="?page=2">. With all of the other variables in the session...
6
1832
by: Jeremy Felt | last post by:
Newbie here. I'm sure I'm missing something EXTREMELY simple, but an hour of searching has led to nothing. I'm playing around with ajax and trying to pass a variable to a function. If I do: onclick="myFunction(12345)"
112
5425
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
5
2204
by: Twayne | last post by:
Hi, If ever a newbie wants to know how much he has to learn yet, he only has to look here<g>!! ANYway: PHP 5.2.5; XP Pro SP2+, local Apache Server My actual question is: How do I get a POST variable into a session variable so I can use it in any other page I want to use it in? Apparently posted variables are only available in the Form page in
0
8476
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8914
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8820
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6223
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5695
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4224
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.