By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

variable and string simple question

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.