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

variable and string simple question

Hi,

this is my code:

<?php
$i=1;
$g1R=10;
$g2R=20;
$g3R=30;
switch ('$g$iR'): --> this syntax does not work!!!
case 10:
echo "it worked";
break;
default:
echo "it did not work";
endswitch;;
?>

I define $g{1,2,3}R as variables and I want to use the switch on them
with $i setting the value of which one of the three will be used. how
do I write the term inside the switch condition?

thanks in advance

Jul 17 '05 #1
6 1100
On 22 May 2005 11:08:46 -0700, inonzuk wrote:
I define $g{1,2,3}R as variables and I want to use the switch on them
with $i setting the value of which one of the three will be used.


$i = 1;
$gR = array( 1 => 10, 20, 30 );
switch ( $gR[$i] )
{
case 10: break;
default: break;
}
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2
inonzuk wrote:
Hi,

this is my code:

<?php
$i=1;
$g1R=10;
$g2R=20;
$g3R=30;
switch ('$g$iR'): --> this syntax does not work!!!


switch(${"g{$i}R"})...
JW

Jul 17 '05 #3
On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:
switch(${"g{$i}R"})...


Yeah, but *really*!
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
Ewoud Dronkert wrote:
On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:
switch(${"g{$i}R"})...


Yeah, but *really*!


Really what? What doesn't look good to you does not mean that it isn't
possible.

It's now up to the OP to chose between your suggestion and the original
approach he wanted to implement.
JW

Jul 17 '05 #5
Janwillem Borleffs wrote:
Ewoud Dronkert wrote:
On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:
switch(${"g{$i}R"})...
Yeah, but *really*!


Really what? What doesn't look good to you does not mean that it

isn't possible.
There are *lots* of things that are *possible* in PHP that are very bad
from a programmatic point of view; that doesn't mean that they should
be used!!
It's now up to the OP to chose between your suggestion and the original approach he wanted to implement.


The problem is, I'm guessing you're a pretty good programmer, and you
know how to do things sensibly, as well as how to do clever tricks like
that.

Judging by the OP's original code snippet, he probably has the attidue
that "arrays are more trouble than they're worth, and take more
typing", or may not even know what arrays are, and will use your
suggestion because it solves his immediate problem without having to
change the rest of his code.

And then he'll go off and write all his PHP scripts using horrible
convoluted expressions like that, rather than taking the time to learn
how to do things properly. Which will lead to his needing to use even
more complicated trickery to fix things later down the line (and
probably use eval() as well!)

IMO, it's the "responsibility" of people who know what they're doing to
encourage good programming practice, or at the very least explain that
things like the above are to be avoided! But that's just my opinion.

--
Oli

Jul 17 '05 #6
Janwillem Borleffs wrote:
What doesn't look good to you does not mean that it isn't possible.
Of course. Just trying to point out to the OP that there was a simpler, more
general solution, easier to maintain and expand, by using a different
concept that for forementioned reasons, yes, did look better to me. I just
*know* it looks better to you too :)
It's now up to the OP to chose between your suggestion and the original
approach he wanted to implement.


Let's hope he will choose wisely.

E.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #7

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

Similar topics

7
by: NS Develop | last post by:
What's wrong with the following: void SomeClass::someMethod(void) { A *ptrA = NULL; ptrA = &A(); .... }
3
by: Larry L | last post by:
This is a novice question, but I'm stumped. I have a variable with a bunch of text assigned to it. Now I want to email the value of the variable. I have a simple form that asks for their name and...
2
by: kaiser | last post by:
Hello I am trying to get the last lines value in a text file and display it on screen / read it into a variable. When I run the following code in a console I get the following error "Use...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
1
by: Hose B | last post by:
HI all, I have a legacy app in which users can pick various page templates. There is a template preview dialog. It works such that they view a list of icons in an asp page that represent each...
4
by: Friday | last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of dot.net (and all things Windows, for that matter). As part of an experiment (to learn enough ASP/VB.net to port a series of ...
4
by: thomasp | last post by:
In the code below I have a function that tests if a file exists. It takes a variable named strFileName, simple enough. My question is, is there a way to pass it a variable with another name as...
10
by: Phillip Vong | last post by:
Newbie learning in VB.NET 2. I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link and I want to use this querystring over and over again through out the page. Example....
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
8
by: Tuxedo | last post by:
How can I modify any one of these global variable identified via a function argument? var x1 = "bla"; var x2 = "bla"; var x3 = "bla"; function modify(variable) { ???? ???? = "blabla":
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.