dynamically naming PHP vars on the fly?
dynamically naming PHP vars on the fly?
am 06.08.2009 00:17:26 von Govinda
HI all
One thing I have been working around but now would love to just do it
finally (and save workaround/longer code hassle) is when:
I need to be able to create a variable (name it, and assign it a
value) whose name is built up from a fixed string concatenated with
another string which comes from the value of another (already set)
variable.
Ie:
I want to do this:
(I am just assuming it won't work; I haven't even tried it yet)
$var1='apple';
$Fruit_$var1="organic";
echo "$Fruit_apple"; // I want this to return "organic"
Or how are you guys dynamically naming PHP vars on the fly?
------------
John Butler (Govinda)
govinda.webdnatalk@gmail.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: dynamically naming PHP vars on the fly?
am 06.08.2009 00:24:56 von Ralph Deffke
u can "dynamical" chosse a variable name with a variable using a "douple" $
charachter eg.
$a = "this is a":
$b = "a";
echo $$b; outputs "this is a"
this is an all open feature to u
have fun
ralph
"Govinda" wrote in message
news:A215E849-2602-4CB3-9DA7-718FF047A187@gmail.com...
> HI all
>
> One thing I have been working around but now would love to just do it
> finally (and save workaround/longer code hassle) is when:
>
> I need to be able to create a variable (name it, and assign it a
> value) whose name is built up from a fixed string concatenated with
> another string which comes from the value of another (already set)
> variable.
>
> Ie:
>
> I want to do this:
> (I am just assuming it won't work; I haven't even tried it yet)
>
> $var1='apple';
> $Fruit_$var1="organic";
> echo "$Fruit_apple"; // I want this to return "organic"
>
> Or how are you guys dynamically naming PHP vars on the fly?
>
> ------------
> John Butler (Govinda)
> govinda.webdnatalk@gmail.com
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: dynamically naming PHP vars on the fly?
am 06.08.2009 00:25:50 von Martin Scotta
You can use variable variables
$nombre =3D 'Martin';
$name =3D 'nombre';
echo $$name; # ===3D Martin
You can make more complicated statements with this technique.
$var1 =3D 'apple';
${ 'Fruit_' . $var1 } =3D 'organic';
echo $Fruit_apple; // here you are
When your statements are complex use the ${ } syntax.
I often use this for hidden global variables.
${ 'try to use this variable directly' } =3D 'something';
print_r( get_defined_vars() ); # [try to use this variable directly]
=3D> something
On Wed, Aug 5, 2009 at 7:17 PM, Govinda wrote=
:
> HI all
>
> One thing I have been working around but now would love to just do it
> finally (and save workaround/longer code hassle) is when:
>
> I need to be able to create a variable (name it, and assign it a value)
> whose name is built up from a fixed string concatenated with another stri=
ng
> which comes from the =A0value of another (already set) variable.
>
> Ie:
>
> I want to do this:
> (I am just assuming it won't work; I haven't even tried it yet)
>
> $var1=3D'apple';
> $Fruit_$var1=3D"organic";
> echo "$Fruit_apple"; // I want this to return "organic"
>
> Or how are you guys dynamically naming PHP vars on the fly?
>
> ------------
> John Butler (Govinda)
> govinda.webdnatalk@gmail.com
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--=20
Martin Scotta
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: dynamically naming PHP vars on the fly?
am 06.08.2009 00:28:36 von Jerry Wilborn
--001636458c0e2a776a04706c8609
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
http://us2.php.net/manual/en/language.variables.variable.php
Jerry Wilborn
jerrywilborn@gmail.com
On Wed, Aug 5, 2009 at 5:17 PM, Govinda wrote:
> HI all
>
> One thing I have been working around but now would love to just do it
> finally (and save workaround/longer code hassle) is when:
>
> I need to be able to create a variable (name it, and assign it a value)
> whose name is built up from a fixed string concatenated with another string
> which comes from the value of another (already set) variable.
>
> Ie:
>
> I want to do this:
> (I am just assuming it won't work; I haven't even tried it yet)
>
> $var1='apple';
> $Fruit_$var1="organic";
> echo "$Fruit_apple"; // I want this to return "organic"
>
> Or how are you guys dynamically naming PHP vars on the fly?
>
> ------------
> John Butler (Govinda)
> govinda.webdnatalk@gmail.com
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--001636458c0e2a776a04706c8609--
Re: dynamically naming PHP vars on the fly?
am 06.08.2009 02:01:24 von Govinda
> You can use variable variables
> ...
> You can make more complicated statements with this technique.
>
> $var1 = 'apple';
> ${ 'Fruit_' . $var1 } = 'organic';
> echo $Fruit_apple; // here you are
thank you all 3, for your help!
That was just what I wanted!
-Govinda
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: dynamically naming PHP vars on the fly?
am 06.08.2009 09:20:54 von news.NOSPAM.0ixbtqKe
On Wed, 5 Aug 2009 16:17:26 -0600, Govinda wrote:
> I want to do this:
> (I am just assuming it won't work; I haven't even tried it yet)
>
> $var1='apple';
> $Fruit_$var1="organic";
> echo "$Fruit_apple"; // I want this to return "organic"
>
> Or how are you guys dynamically naming PHP vars on the fly?
Others have mentioned variable variables. While I have
used those, I tend to prefer arrays:
$var1 = 'apple';
$fruits[$var1] = 'organic';
echo $fruits[$var1];
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: dynamically naming PHP vars on the fly?
am 06.08.2009 20:42:49 von Govinda
--Apple-Mail-2-694030347
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes
Content-Transfer-Encoding: 7bit
> Others have mentioned variable variables. While I have
> used those, I tend to prefer arrays:
>
> $var1 = 'apple';
> $fruits[$var1] = 'organic';
> echo $fruits[$var1];
>
>
> /Nisse
ah, yes, I see that too, now. Thanks for reminding me about arrays.
(You must be quite adept at arrays. )
(And to give you credit Nisse, I ended up going with your solution
('knock on wood'.. it is not all done yet) from the other php-db list
(building an array from data returned from the 3 union all selects-
query).)
-G
--Apple-Mail-2-694030347--
Re: Re: dynamically naming PHP vars on the fly?
am 08.08.2009 14:52:42 von news.NOSPAM.0ixbtqKe
On Thu, 6 Aug 2009 12:42:49 -0600, Govinda wrote:
>> Others have mentioned variable variables. While I have
>> used those, I tend to prefer arrays:
>
> ah, yes, I see that too, now. Thanks for reminding me about arrays.
> (You must be quite adept at arrays. )
I don't know, but I often stuff things into array,
such as the following snippet:
.... while ($r = mysql_fetch_row ($res)) {
$news_paged[$page][$r[0]]['short_desc'] = $r[1];
if (isset ($r[2]))
$news_paged[$page][$r[0]]['long_desc'] = $r[2];
....
I wonder if there's a noticable difference in performance
between variable variables and arrays... I'm guessing it's
small enough to be of no concern.
> (And to give you credit Nisse, I ended up going with your solution
> ('knock on wood'.. it is not all done yet) from the other php-db list
> (building an array from data returned from the 3 union all selects-
> query).)
I'm a total newbie when it comes to SQL beyond simple SELECTs.
If there's a noticable gain to be made from limiting the number
of rows returned from the database, then that is probably worth
persuing. Otherwise, I'd do it in PHP.
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: dynamically naming PHP vars on the fly?
am 08.08.2009 15:01:42 von Ralph Deffke
of course u can do this by sql.
see this
SELECT *
FROM `sometable`
LIMIT 0 , 30
gives u max 30 records if existstarting at record 0
on the next request u could say
SELECT *
FROM `sometable`
LIMIT 30 , 30
giving u max 30 recotds starting at record 30
ralph
ralph_deffke@yahoo.de
"Nisse Engström" wrote in message
news:EB.35.03345.5F47D7A4@pb1.pair.com...
> On Thu, 6 Aug 2009 12:42:49 -0600, Govinda wrote:
>
> >> Others have mentioned variable variables. While I have
> >> used those, I tend to prefer arrays:
> >
> > ah, yes, I see that too, now. Thanks for reminding me about arrays.
> > (You must be quite adept at arrays. )
>
> I don't know, but I often stuff things into array,
> such as the following snippet:
>
> ... while ($r = mysql_fetch_row ($res)) {
> $news_paged[$page][$r[0]]['short_desc'] = $r[1];
> if (isset ($r[2]))
> $news_paged[$page][$r[0]]['long_desc'] = $r[2];
> ...
>
>
> I wonder if there's a noticable difference in performance
> between variable variables and arrays... I'm guessing it's
> small enough to be of no concern.
>
> > (And to give you credit Nisse, I ended up going with your solution
> > ('knock on wood'.. it is not all done yet) from the other php-db list
> > (building an array from data returned from the 3 union all selects-
> > query).)
>
> I'm a total newbie when it comes to SQL beyond simple SELECTs.
> If there's a noticable gain to be made from limiting the number
> of rows returned from the database, then that is probably worth
> persuing. Otherwise, I'd do it in PHP.
>
>
> /Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: dynamically naming PHP vars on the fly?
am 08.08.2009 15:45:36 von news.NOSPAM.0ixbtqKe
On Sat, 8 Aug 2009 15:01:42 +0200, "Ralph Deffke" wrote:
> of course u can do this by sql.
>
> see this
> SELECT *
> FROM `sometable`
> LIMIT 0 , 30
>
> gives u max 30 records if existstarting at record 0
>
> on the next request u could say
> SELECT *
> FROM `sometable`
> LIMIT 30 , 30
>
> giving u max 30 recotds starting at record 30
Yup. I do that a lot. But the original problem was
about a UNION of three tables. That's when my head
go dizzy. (And that's not because of the bottle of
Guinness I just had for lunch).
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: dynamically naming PHP vars on the fly?
am 08.08.2009 16:25:24 von Ralph Deffke
but then ur byond simple select, man, I do hate unions and try to avoid
those.
"Nisse Engström" wrote in message
news:8B.50.40613.C518D7A4@pb1.pair.com...
> On Sat, 8 Aug 2009 15:01:42 +0200, "Ralph Deffke" wrote:
>
> > of course u can do this by sql.
> >
> > see this
> > SELECT *
> > FROM `sometable`
> > LIMIT 0 , 30
> >
> > gives u max 30 records if existstarting at record 0
> >
> > on the next request u could say
> > SELECT *
> > FROM `sometable`
> > LIMIT 30 , 30
> >
> > giving u max 30 recotds starting at record 30
>
> Yup. I do that a lot. But the original problem was
> about a UNION of three tables. That's when my head
> go dizzy. (And that's not because of the bottle of
> Guinness I just had for lunch).
>
>
> /Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: dynamically naming PHP vars on the fly?
am 08.08.2009 18:37:53 von news.NOSPAM.0ixbtqKe
On Sat, 8 Aug 2009 16:25:24 +0200, "Ralph Deffke" wrote:
> but then ur byond simple select, man, I do hate unions
> and try to avoid those.
Hear, hear!
I'll look into that stuff in my next life, perhaps.
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php