Re: [PHP] Question on variable variables
This would be the logical way to do it (as I see it): define('MY','pre_'); $a = dog; $new_var = MY.$a; $$new_var and $pre_dog are the same :) --Joe On Fri, Nov 16, 2001 at 09:47:12PM -0500, Jeff Lewis wrote: I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff Joe Stump [EMAIL PROTECTED] How would this sentence be different if pi equaled 3? msg40409/pgp0.pgp Description: PGP signature
Re: [PHP] Question on variable variables
It seems to me that the use of a temp variable may be the clearest solution, and I doubt any overhead would matter really. $a = dog; $tmp = MY.$a; //$tmp = MYdog; So the following two would be the same: $$tmp = Spot; $MYdog = Spot; Good Luck, Jason Garber IonZoft.com At 09:47 PM 11/16/2001 -0500, Jeff Lewis wrote: I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Question on variable variables
Thanks Jason and Christoper for replying, here is the solution I came up with. It takes into account that there could be multiple instances of what I'm looking for on the same line - got it working late last night: while (preg_match (/yabb\s+(\w+)/,$curline,$tags)) $curline = preg_replace(/yabb\s+$tags[1]/,${$tags[1]},$curline); Jeff - Original Message - From: Jason G. [EMAIL PROTECTED] To: Jeff Lewis [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, November 17, 2001 7:02 AM Subject: Re: [PHP] Question on variable variables It seems to me that the use of a temp variable may be the clearest solution, and I doubt any overhead would matter really. $a = dog; $tmp = MY.$a; //$tmp = MYdog; So the following two would be the same: $$tmp = Spot; $MYdog = Spot; Good Luck, Jason Garber IonZoft.com At 09:47 PM 11/16/2001 -0500, Jeff Lewis wrote: I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Question on variable variables
have you tried /e modifier? preg_replace(/yabb\s+$tags[1]/e,'${$1}', $curline); it's just a tip. - Original Message - From: Jeff Lewis [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, November 17, 2001 2:25 PM Subject: Re: [PHP] Question on variable variables Thanks Jason and Christoper for replying, here is the solution I came up with. It takes into account that there could be multiple instances of what I'm looking for on the same line - got it working late last night: while (preg_match (/yabb\s+(\w+)/,$curline,$tags)) $curline = preg_replace(/yabb\s+$tags[1]/,${$tags[1]},$curline); Jeff - Original Message - From: Jason G. [EMAIL PROTECTED] To: Jeff Lewis [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, November 17, 2001 7:02 AM Subject: Re: [PHP] Question on variable variables It seems to me that the use of a temp variable may be the clearest solution, and I doubt any overhead would matter really. $a = dog; $tmp = MY.$a; //$tmp = MYdog; So the following two would be the same: $$tmp = Spot; $MYdog = Spot; Good Luck, Jason Garber IonZoft.com At 09:47 PM 11/16/2001 -0500, Jeff Lewis wrote: I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Question on variable variables
Oops... have you tried /e modifier? preg_replace(/yabb\s+$tags[1]/e,'${$1}', $curline); preg_replace(/yabb\s+(\w+)/e,'${$1}', $curline); it's just a tip. - Original Message - From: Jeff Lewis [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Saturday, November 17, 2001 2:25 PM Subject: Re: [PHP] Question on variable variables Thanks Jason and Christoper for replying, here is the solution I came up with. It takes into account that there could be multiple instances of what I'm looking for on the same line - got it working late last night: while (preg_match (/yabb\s+(\w+)/,$curline,$tags)) $curline = preg_replace(/yabb\s+$tags[1]/,${$tags[1]},$curline); Jeff - Original Message - From: Jason G. [EMAIL PROTECTED] To: Jeff Lewis [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Saturday, November 17, 2001 7:02 AM Subject: Re: [PHP] Question on variable variables It seems to me that the use of a temp variable may be the clearest solution, and I doubt any overhead would matter really. $a = dog; $tmp = MY.$a; //$tmp = MYdog; So the following two would be the same: $$tmp = Spot; $MYdog = Spot; Good Luck, Jason Garber IonZoft.com At 09:47 PM 11/16/2001 -0500, Jeff Lewis wrote: I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Question on variable variables
I've a question regarding variable variable I was hoping someone could help me with: All the examples in the manual have the entire variable name being variable e.g. $a = hello and $$a being the same as $hello What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. Jeff
Re: [PHP] Question on variable variables
On Fri, 16 Nov 2001, Jeff Lewis wrote: What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. So what you want is a base name with a vaiably-changing portion concatenated? Gotcha! // you variable base name $myVarBase = MY; $a = cat; $something1 = This Var Likes Cats; $b = dog; $something2 = This Var Likes Dogs; $myVarName1 = $myVarBase . $a; $myVarName2 = $myVarBase . $b; // The same as $Mycat = This Var Likes Cats; ${$myVarName1} = $something1; // The same as $Mydog = This Var Likes Dogs; ${$myVarName2} = $something2; g.luck ~Chris /\ \ / September 11, 2001 X We Are All New Yorkers / \ rm -rf /bin/laden -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Question on variable variables
Yes trying to get this to work: $curline = preg_replace(/yabb\s+(\w+)/,$$1,$curline); So for the current line I am looking for something like yabb copyright I want to replace that with the contents of $copyright. Can variable variables be used in regular expressions? Jeff - Original Message - From: Christopher William Wesley [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: Jeff Lewis [EMAIL PROTECTED] Sent: Friday, November 16, 2001 10:38 PM Subject: Re: [PHP] Question on variable variables On Fri, 16 Nov 2001, Jeff Lewis wrote: What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. So what you want is a base name with a vaiably-changing portion concatenated? Gotcha! // you variable base name $myVarBase = MY; $a = cat; $something1 = This Var Likes Cats; $b = dog; $something2 = This Var Likes Dogs; $myVarName1 = $myVarBase . $a; $myVarName2 = $myVarBase . $b; // The same as $Mycat = This Var Likes Cats; ${$myVarName1} = $something1; // The same as $Mydog = This Var Likes Dogs; ${$myVarName2} = $something2; g.luck ~Chris /\ \ / September 11, 2001 X We Are All New Yorkers / \ rm -rf /bin/laden -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Question on variable variables
You sure can. You can use variable variables wherever you can use regular variables. They're really no different than regular variables, except you have to keep track of what the variable will be named while coding :) ~Chris /\ \ / September 11, 2001 X We Are All New Yorkers / \ rm -rf /bin/laden On Fri, 16 Nov 2001, Jeff Lewis wrote: Yes trying to get this to work: $curline = preg_replace(/yabb\s+(\w+)/,$$1,$curline); So for the current line I am looking for something like yabb copyright I want to replace that with the contents of $copyright. Can variable variables be used in regular expressions? Jeff - Original Message - From: Christopher William Wesley [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: Jeff Lewis [EMAIL PROTECTED] Sent: Friday, November 16, 2001 10:38 PM Subject: Re: [PHP] Question on variable variables On Fri, 16 Nov 2001, Jeff Lewis wrote: What I need to do, however, is append a variable portion to a constant prefix. So I have a set of variables that are named $MYdog, $MYcat etc. and I need to do $a = dog ${MY$a} being the same as $MYdog Can this be done, and if so - how? I can't get it to work. So what you want is a base name with a vaiably-changing portion concatenated? Gotcha! // you variable base name $myVarBase = MY; $a = cat; $something1 = This Var Likes Cats; $b = dog; $something2 = This Var Likes Dogs; $myVarName1 = $myVarBase . $a; $myVarName2 = $myVarBase . $b; // The same as $Mycat = This Var Likes Cats; ${$myVarName1} = $something1; // The same as $Mydog = This Var Likes Dogs; ${$myVarName2} = $something2; g.luck ~Chris /\ \ / September 11, 2001 X We Are All New Yorkers / \ rm -rf /bin/laden -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]