Welcome to MobyThreads.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in
All support for the MobyThreads Threaded phpBB MOD can now be found on welsolutions at this forum

PHP: Variable as part of a variable

 
   Web Hosting and Web Master Forums (Home) -> Webmaster RSS
Next:  Make Extra Money Easy  
Author Message
contact1

External


Since: Sep 01, 2003
Posts: 83



(Msg. 1) Posted: Thu Sep 18, 2003 4:52 am
Post subject: PHP: Variable as part of a variable
Archived from groups: alt>www>webmaster (more info?)

Here's a confusing one.

I want to feed a PHP script a variable value that will be used as part
of a variable name when the script runs.

$1a = "Something";
$1b = "Something else";
$1c = "Something else entirely";

// Run /danielsscript?code=b

print (" Blah blah blah: $1$code "); // Variables somehow merge as one

// Result: "Blah blah blah: Something else"

Any idea on how to manage it? Thanks!

--
Daniel Ruscoe
http://www.dannyruscoe.co.uk

 >> Stay informed about: PHP: Variable as part of a variable 
Back to top
Login to vote
sp_bhuisman

External


Since: Jun 27, 2003
Posts: 571



(Msg. 2) Posted: Thu Sep 18, 2003 4:53 am
Post subject: Re: PHP: Variable as part of a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 18 Sep 2003 01:52:59 +0000 (UTC), Daniel Ruscoe
<contact.DeleteThis@website.plz> wrote:

 > Here's a confusing one.
 >
 > I want to feed a PHP script a variable value that will be used as part
 > of a variable name when the script runs.
 >
 > $1a = "Something";
 > $1b = "Something else";
 > $1c = "Something else entirely";
 >
 > // Run /danielsscript?code=b
 >
 > print (" Blah blah blah: $1$code "); // Variables somehow merge as one
 >
 > // Result: "Blah blah blah: Something else"
 >
 > Any idea on how to manage it? Thanks!

Just use a text-keyed array. Instead of:

$1a = "Something";
$1b = "Something else";
$1c = "Something else entirely";

use:

$txt['a'] = "Something";
$txt['b'] = "Something else";
$txt['c'] = "Something else entirely";

// Run /danielsscript?code=b

print (" Blah blah blah: {$txt[$code]}"); // $code is the key to the
requested array value.

// Result: "Blah blah blah: Something else"

Easy peasy. BTW, these kinds of questions are better asked in
<a style='text-decoration: underline;' href="news://comp.lang.php" target="_blank">news://comp.lang.php</a>


Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollory that nothing is ridiculous.
- <a style='text-decoration: underline;' href="http://www.greywyvern.com" target="_blank">http://www.greywyvern.com</a> - ORCA - Camouflaged PHP Web Scripts.<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: PHP: Variable as part of a variable 
Back to top
Login to vote
contact1

External


Since: Sep 01, 2003
Posts: 83



(Msg. 3) Posted: Thu Sep 18, 2003 5:38 am
Post subject: Re: PHP: Variable as part of a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <oprvoak5jpv0in9v.RemoveThis@news.ican.net>, GreyWyvern says...

 > Just use a text-keyed array. Instead of:
 >
 > $1a = "Something";
 > $1b = "Something else";
 > $1c = "Something else entirely";
 >
 > use:
 >
 > $txt['a'] = "Something";
 > $txt['b'] = "Something else";
 > $txt['c'] = "Something else entirely";
 >
 > // Run /danielsscript?code=b
 >
 > print (" Blah blah blah: {$txt[$code]}"); // $code is the key to the
 > requested array value.

Perfect! I tried but I couldn't find anything that useful on Google.

 > Easy peasy. BTW, these kinds of questions are better asked in
<font color=purple> > <a style='text-decoration: underline;' href="news://comp.lang.php</font" target="_blank">news://comp.lang.php</font</a>>

You're right, but it's gone 3:30 in the morning and I didn't have the
energy to go searching for other groups Smile

Besides, I like the people here.

--
Daniel Ruscoe
<a style='text-decoration: underline;' href="http://www.dannyruscoe.co.uk" target="_blank">http://www.dannyruscoe.co.uk</a><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: PHP: Variable as part of a variable 
Back to top
Login to vote
discooctopus

External


Since: Jul 01, 2003
Posts: 34



(Msg. 4) Posted: Thu Sep 18, 2003 9:44 am
Post subject: Re: Variable as part of a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Daniel Ruscoe wrote:
 > Here's a confusing one.
 >
 > I want to feed a PHP script a variable value that will be used as part
 > of a variable name when the script runs.
 >
 > $1a = "Something";
 > $1b = "Something else";
 > $1c = "Something else entirely";
 >
 > // Run /danielsscript?code=b
 >
 > print (" Blah blah blah: $1$code "); // Variables somehow merge as one
 >
 > // Result: "Blah blah blah: Something else"
 >
 > Any idea on how to manage it? Thanks!

this may give you an idea for doing this.
(btw. i dont think you are allowed to have variable names that start with a
numeric)

<?php
$a1a = 'Something';
$a1b = 'Something else';
$a1c = 'Something else entirely';

$code = 'b';

$vvv = "a1" . $code;

echo $vvv . '<br>' . $$vvv;

?><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: PHP: Variable as part of a variable 
Back to top
Login to vote
davidvb

External


Since: Jun 28, 2003
Posts: 114



(Msg. 5) Posted: Thu Sep 18, 2003 8:11 pm
Post subject: Re: PHP: Variable as part of a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

GreyWyvern wrote:
 > On Thu, 18 Sep 2003 01:52:59 +0000 (UTC), Daniel Ruscoe
 > <contact.TakeThisOut@website.plz> wrote:
 >
  >> Here's a confusing one.
  >>
  >> I want to feed a PHP script a variable value that will be used as part
  >> of a variable name when the script runs.
  >>
  >> $1a = "Something";
  >> $1b = "Something else";
  >> $1c = "Something else entirely";
  >>
  >> // Run /danielsscript?code=b
  >>
  >> print (" Blah blah blah: $1$code "); // Variables somehow merge as one
  >>
  >> // Result: "Blah blah blah: Something else"
  >>
  >> Any idea on how to manage it? Thanks!
 >
 >
 > Just use a text-keyed array. Instead of:
 >
 > $1a = "Something";
 > $1b = "Something else";
 > $1c = "Something else entirely";
 >
 > use:
 >
 > $txt['a'] = "Something";
 > $txt['b'] = "Something else";
 > $txt['c'] = "Something else entirely";
 >
 > // Run /danielsscript?code=b
 >
 > print (" Blah blah blah: {$txt[$code]}"); // $code is the key to the
 > requested array value.
 >
 > // Result: "Blah blah blah: Something else"
 >
 > Easy peasy. BTW, these kinds of questions are better asked in
<font color=purple> > <a style='text-decoration: underline;' href="news://comp.lang.php</font" target="_blank">news://comp.lang.php</font</a>>

Yes, 'tis the easy way.

I think you could also get the same effect using syntax similar to Dan's
(for the sake of argument + alternative ways of doing things + to show off):

$1a = "Something";
$1b = "Something else";
$1c = "Something else entirely";

// Run /danielsscript?code=b

print (" Blah blah blah: ${"1" . $code} "); // Variables somehow merge
as one

// Result: "Blah blah blah: Something else"

Should do the trick.

Alternatively you could have

$code = "1" . $code;

// and

print (" Blah blah blah: $$code "); // Variables somehow merge as one

Anyhow, <a style='text-decoration: underline;' href="http://au2.php.net/variables.variable" target="_blank">http://au2.php.net/variables.variable</a> is a fairly good reference
on how to generate variables on the fly.

</2c><!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: PHP: Variable as part of a variable 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
CSS wizardry needed - part 2 - I'd been redesigning one of my web sites when I read on this group about the advantages of using CSS to do the job of tables for layout. Although I already had a working tables version of my site layout, I thought I'd give it a go, so would like your..

How to get banner advertisers.. part 2 - Sorry about the mispost.... I have a website that is very niche market. I have never had advertisers before but would like to generate a few bucks to help pay for the site. My site drawings in a very specific user. I get 5000+ unique vistors per month...
   Web Hosting and Web Master Forums (Home) -> Webmaster All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]