473,396 Members | 2,011 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,396 software developers and data experts.

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 1845
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
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...
10
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:...
134
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...
5
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...
23
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
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
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...
6
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:...
112
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...
5
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...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.