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

Eval, Variable Variables problem

Ted
I have a small pre-determined number of possible values for a variable, and
I would like to set a value for the corresponding 'switch type' variable.

For example, if $x is 14, I want to execute the following statement:
$switch_14 = "on";

In general, I want something like this:
$switch_.$x = "on";

I know I could build a switch/case structure since the values for $x are
known and there are only a dozen or so of them, but it would seem more
elegant and/or compact to do it another way.

I've looked at eval() and variable variables and can't seem to get anything
to work.
I've also read the quote "If eval is the answer, you're asking the wrong
question".

Thanks for any help you can provide.

T
Jan 3 '07 #1
4 1586
..oO(Ted)
>I have a small pre-determined number of possible values for a variable, and
I would like to set a value for the corresponding 'switch type' variable.

For example, if $x is 14, I want to execute the following statement:
$switch_14 = "on";

In general, I want something like this:
$switch_.$x = "on";
You're looking for an array:

$switch[$x] = TRUE;

Micha
Jan 3 '07 #2
Rik
Ted wrote:
I have a small pre-determined number of possible values for a
variable, and I would like to set a value for the corresponding
'switch type' variable.

For example, if $x is 14, I want to execute the following statement:
$switch_14 = "on";

In general, I want something like this:
$switch_.$x = "on";

I know I could build a switch/case structure since the values for $x
are known and there are only a dozen or so of them, but it would seem
more elegant and/or compact to do it another way.

I've looked at eval() and variable variables and can't seem to get
anything to work.
I've also read the quote "If eval is the answer, you're asking the
wrong question".

Thanks for any help you can provide.

As said, you probably want an array. However, if you want to stick with
your way, I think this will work (untested):

${'switch_'.$i} = 'on';
--
Rik Wasmus
Jan 3 '07 #3
Just a suggestion.. Why not use array?

$switches = array();
$switches[$x] = 'on';

That way it's cleaner, and no need to change any
switches or cases values later on when you
decided to change the $x to char or word for example.

Hendri Kurniawan
Ted wrote:
I have a small pre-determined number of possible values for a variable, and
I would like to set a value for the corresponding 'switch type' variable.

For example, if $x is 14, I want to execute the following statement:
$switch_14 = "on";

In general, I want something like this:
$switch_.$x = "on";

I know I could build a switch/case structure since the values for $x are
known and there are only a dozen or so of them, but it would seem more
elegant and/or compact to do it another way.

I've looked at eval() and variable variables and can't seem to get anything
to work.
I've also read the quote "If eval is the answer, you're asking the wrong
question".

Thanks for any help you can provide.

T

Jan 3 '07 #4
you may do:

$x = 1;
$name = "var_{$x}";
$$name = "On";

echo $var_1;// On

Jan 4 '07 #5

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

Similar topics

2
by: Mark Richards | last post by:
Perhaps this has been covered elsewhere, but can't find a reference to it in FAQ and through diligent searching. In some languages, it's possible to perform a runtime eval() that essentially...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
13
by: Stephen Kay | last post by:
Is there a way to redirect every single page on an existing web site through a php function? In other words, say I have a whole functional HTML web site, never written to use any php. Now I...
5
by: R | last post by:
Hi All, I'm using eval and arrays in foreach and have trouble with adding elements to them - I'm talking about the '' operator. My code is: // creates arrays with the names of columns in...
8
by: rdrink | last post by:
n.n.h. (noob needs help) Ok, I've been beating my head against this for a day... time to ask others. To explain things as simply as possible: I am trying to use eval() to evaluate some simple...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of 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
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: 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:
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
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
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...
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.