Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10212 > unrolled thread

Variable variables?

Started byScott Johnson <noonehome@chalupasworld.com>
First post2013-07-25 19:56 -0700
Last post2013-07-27 09:37 +0200
Articles 11 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-25 19:56 -0700
    Re: Variable variables? Jerry Stuckle <jstucklex@attglobal.net> - 2013-07-26 06:56 -0400
      Re: Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-26 12:39 -0700
        Re: Variable variables? Jerry Stuckle <jstucklex@attglobal.net> - 2013-07-27 09:48 -0400
          Re: Variable variables? Tim Streater <timstreater@greenbee.net> - 2013-07-27 15:45 +0100
            Re: Variable variables? The Natural Philosopher <tnp@invalid.invalid> - 2013-07-27 15:46 +0100
              Re: Variable variables? Tim Streater <timstreater@greenbee.net> - 2013-07-27 15:53 +0100
    Re: Variable variables? Luuk <luuk@invalid.lan> - 2013-07-26 22:04 +0200
      Re: Variable variables? Scott Johnson <noonehome@chalupasworld.com> - 2013-07-26 20:27 -0700
    Re: Variable variables? Christoph Michael Becker <cmbecker69@arcor.de> - 2013-07-26 22:17 +0200
      Re: Variable variables? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-07-27 09:37 +0200

#10212 — Variable variables?

FromScott Johnson <noonehome@chalupasworld.com>
Date2013-07-25 19:56 -0700
SubjectVariable variables?
Message-ID<ksso5i$u2t$1@dont-email.me>
I have a situation which I 'think' I need to use variable variables and 
not sure how to go about it.


I have several <select multiple> elements on a search form with their 
names created dynamically and the options created dynamically.

$parent holds the name such as "Women", "Mens" etc..
$c_id holds category_id and is sent off as the 'value' of the option.

This form is self calling and an array is created for the multiple 
option selected.

Women => array([0]=>20,[1]=>21,...)
Mens => array([0]=>32,[1]=>35,...)

I am using in_array() to check returned posted values against an id.

How do I reference that array by using the value of $parent.

if $parent is 'Women' I need to reference the array 'Women'

Here is a nutshell of the algorithm.

foreach($parent_ary as $parent) {
	echo "<select {$parent}[] multiple>";
	foreach($category_ary as $c_id=>$category) {
		if(in_array($c_id, array of posted value)) {
			// Create select option
		} else {
			// Create regular option
		}
	}
	echo "</select>";
}

I hope this makes sense and yes I did RTM but could not transpose what 
they where explaining to what I need to do.

Thanks
Scotty

[toc] | [next] | [standalone]


#10216

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-07-26 06:56 -0400
Message-ID<kstk8j$p0q$1@dont-email.me>
In reply to#10212
On 7/25/2013 10:56 PM, Scott Johnson wrote:
> I have a situation which I 'think' I need to use variable variables and
> not sure how to go about it.
>
>
> I have several <select multiple> elements on a search form with their
> names created dynamically and the options created dynamically.
>
> $parent holds the name such as "Women", "Mens" etc..
> $c_id holds category_id and is sent off as the 'value' of the option.
>
> This form is self calling and an array is created for the multiple
> option selected.
>
> Women => array([0]=>20,[1]=>21,...)
> Mens => array([0]=>32,[1]=>35,...)
>
> I am using in_array() to check returned posted values against an id.
>
> How do I reference that array by using the value of $parent.
>
> if $parent is 'Women' I need to reference the array 'Women'
>
> Here is a nutshell of the algorithm.
>
> foreach($parent_ary as $parent) {
>      echo "<select {$parent}[] multiple>";
>      foreach($category_ary as $c_id=>$category) {
>          if(in_array($c_id, array of posted value)) {
>              // Create select option
>          } else {
>              // Create regular option
>          }
>      }
>      echo "</select>";
> }
>
> I hope this makes sense and yes I did RTM but could not transpose what
> they where explaining to what I need to do.
>
> Thanks
> Scotty

I'm not sure why you think you need variable variables (another sucky 
idea on PHP's part, IMHO).  Build your array with the field name as the 
key and the value as the data.  Then you can access it as

   foreach ($array as $key=>$value)

If each key contains multiple values, then $value is just another array.

Just be sure to validate your incoming data.  Such constructions are 
more susceptible to be hacked unless you're very careful.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#10218

FromScott Johnson <noonehome@chalupasworld.com>
Date2013-07-26 12:39 -0700
Message-ID<ksuiu7$5hd$1@dont-email.me>
In reply to#10216
On 7/26/2013 3:56 AM, Jerry Stuckle wrote:
> On 7/25/2013 10:56 PM, Scott Johnson wrote:
>> I have a situation which I 'think' I need to use variable variables and
>> not sure how to go about it.
>>
>>
>> I have several <select multiple> elements on a search form with their
>> names created dynamically and the options created dynamically.
>>
>> $parent holds the name such as "Women", "Mens" etc..
>> $c_id holds category_id and is sent off as the 'value' of the option.
>>
>> This form is self calling and an array is created for the multiple
>> option selected.
>>
>> Women => array([0]=>20,[1]=>21,...)
>> Mens => array([0]=>32,[1]=>35,...)
>>
>> I am using in_array() to check returned posted values against an id.
>>
>> How do I reference that array by using the value of $parent.
>>
>> if $parent is 'Women' I need to reference the array 'Women'
>>
>> Here is a nutshell of the algorithm.
>>
>> foreach($parent_ary as $parent) {
>>      echo "<select {$parent}[] multiple>";
>>      foreach($category_ary as $c_id=>$category) {
>>          if(in_array($c_id, array of posted value)) {
>>              // Create select option
>>          } else {
>>              // Create regular option
>>          }
>>      }
>>      echo "</select>";
>> }
>>
>> I hope this makes sense and yes I did RTM but could not transpose what
>> they where explaining to what I need to do.
>>
>> Thanks
>> Scotty
>
> I'm not sure why you think you need variable variables (another sucky
> idea on PHP's part, IMHO).  Build your array with the field name as the
> key and the value as the data.  Then you can access it as
>
>    foreach ($array as $key=>$value)
>
> If each key contains multiple values, then $value is just another array.
>
> Just be sure to validate your incoming data.  Such constructions are
> more susceptible to be hacked unless you're very careful.
>

Well I was thinking of variable variables because.....I ran out of ideas 
and it seemed like a way to go?  ;)

I will give what you suggest a go, seems like a simple enough approach.

Thanks
Scotty

[toc] | [prev] | [next] | [standalone]


#10228

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-07-27 09:48 -0400
Message-ID<kt0imo$2rg$1@dont-email.me>
In reply to#10218
On 7/26/2013 3:39 PM, Scott Johnson wrote:
> On 7/26/2013 3:56 AM, Jerry Stuckle wrote:
>> On 7/25/2013 10:56 PM, Scott Johnson wrote:
>>> I have a situation which I 'think' I need to use variable variables and
>>> not sure how to go about it.
>>>
>>>
>>> I have several <select multiple> elements on a search form with their
>>> names created dynamically and the options created dynamically.
>>>
>>> $parent holds the name such as "Women", "Mens" etc..
>>> $c_id holds category_id and is sent off as the 'value' of the option.
>>>
>>> This form is self calling and an array is created for the multiple
>>> option selected.
>>>
>>> Women => array([0]=>20,[1]=>21,...)
>>> Mens => array([0]=>32,[1]=>35,...)
>>>
>>> I am using in_array() to check returned posted values against an id.
>>>
>>> How do I reference that array by using the value of $parent.
>>>
>>> if $parent is 'Women' I need to reference the array 'Women'
>>>
>>> Here is a nutshell of the algorithm.
>>>
>>> foreach($parent_ary as $parent) {
>>>      echo "<select {$parent}[] multiple>";
>>>      foreach($category_ary as $c_id=>$category) {
>>>          if(in_array($c_id, array of posted value)) {
>>>              // Create select option
>>>          } else {
>>>              // Create regular option
>>>          }
>>>      }
>>>      echo "</select>";
>>> }
>>>
>>> I hope this makes sense and yes I did RTM but could not transpose what
>>> they where explaining to what I need to do.
>>>
>>> Thanks
>>> Scotty
>>
>> I'm not sure why you think you need variable variables (another sucky
>> idea on PHP's part, IMHO).  Build your array with the field name as the
>> key and the value as the data.  Then you can access it as
>>
>>    foreach ($array as $key=>$value)
>>
>> If each key contains multiple values, then $value is just another array.
>>
>> Just be sure to validate your incoming data.  Such constructions are
>> more susceptible to be hacked unless you're very careful.
>>
>
> Well I was thinking of variable variables because.....I ran out of ideas
> and it seemed like a way to go?  ;)
>
> I will give what you suggest a go, seems like a simple enough approach.
>
> Thanks
> Scotty

The salient point here is - you KNOW what names SHOULD be on your form. 
  You may have several fields, but you know what fields are available 
(even if you're only using a subset of those fields).

You need to control what you have on the form.  What you DON'T want is a 
hacker to post his/her own form with data which should not normally be 
available.

OTOH, if you're creating field names dynamically because you don't know 
how many you will have, you should be using an array.  For instance, if 
you're looking for restaurants and want to have checkboxes for various 
nationalities, the nationalities available should be all be in the same 
array.

The reason I think variable variables are sucky is because they almost 
always lead to hard to understand and hard to maintain code.  Data 
validation becomes much more difficult and often leads to holes in the 
code which can be used by hackers - especially when the info is coming 
from the user.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#10229

FromTim Streater <timstreater@greenbee.net>
Date2013-07-27 15:45 +0100
Message-ID<timstreater-1C61A2.15450627072013@news.individual.net>
In reply to#10228
In article <kt0imo$2rg$1@dont-email.me>,
 Jerry Stuckle <jstucklex@attglobal.net> wrote:

> The reason I think variable variables are sucky is because they almost 
> always lead to hard to understand and hard to maintain code.

Quite right.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#10230

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-07-27 15:46 +0100
Message-ID<kt0mf9$pl9$2@news.albasani.net>
In reply to#10229
On 27/07/13 15:45, Tim Streater wrote:
> In article <kt0imo$2rg$1@dont-email.me>,
> Jerry Stuckle <jstucklex@attglobal.net> wrote:
>
>> The reason I think variable variables are sucky is because they 
>> almost always lead to hard to understand and hard to maintain code.
>
> Quite right.
>
I thought that was PERL and regexps?

(I'll get my coat).


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#10231

FromTim Streater <timstreater@greenbee.net>
Date2013-07-27 15:53 +0100
Message-ID<timstreater-1B72D6.15532927072013@news.individual.net>
In reply to#10230
In article <kt0mf9$pl9$2@news.albasani.net>,
 The Natural Philosopher <tnp@invalid.invalid> wrote:

> On 27/07/13 15:45, Tim Streater wrote:
> > In article <kt0imo$2rg$1@dont-email.me>,
> > Jerry Stuckle <jstucklex@attglobal.net> wrote:
> >
> >> The reason I think variable variables are sucky is because they 
> >> almost always lead to hard to understand and hard to maintain code.
> >
> > Quite right.
> >
> I thought that was PERL and regexps?

Yes, we'll be arresting them too.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#10219

FromLuuk <luuk@invalid.lan>
Date2013-07-26 22:04 +0200
Message-ID<3c1bca-p81.ln1@luuk.invalid.lan>
In reply to#10212
On 26-07-2013 04:56, Scott Johnson wrote:
> I have a situation which I 'think' I need to use variable variables and
> not sure how to go about it.
>
>
> I have several <select multiple> elements on a search form with their
> names created dynamically and the options created dynamically.
>
> $parent holds the name such as "Women", "Mens" etc..
> $c_id holds category_id and is sent off as the 'value' of the option.
>
> This form is self calling and an array is created for the multiple
> option selected.
>
> Women => array([0]=>20,[1]=>21,...)
> Mens => array([0]=>32,[1]=>35,...)
>
> I am using in_array() to check returned posted values against an id.
>
> How do I reference that array by using the value of $parent.
>
> if $parent is 'Women' I need to reference the array 'Women'
>
> Here is a nutshell of the algorithm.
>
> foreach($parent_ary as $parent) {
>      echo "<select {$parent}[] multiple>";
>      foreach($category_ary as $c_id=>$category) {
>          if(in_array($c_id, array of posted value)) {
>              // Create select option
>          } else {
>              // Create regular option
>          }
>      }
>      echo "</select>";
> }
>
> I hope this makes sense and yes I did RTM but could not transpose what
> they where explaining to what I need to do.
>
> Thanks
> Scotty

This will return 'test':
$a="test"; $b="a"; print ${$b};

so, if $parent="Woman", then ${$parent} should refer to the $Woman[],
and if $parent="Mens" then ${$parent} should refer to $Mens[]




[toc] | [prev] | [next] | [standalone]


#10221

FromScott Johnson <noonehome@chalupasworld.com>
Date2013-07-26 20:27 -0700
Message-ID<ksveb4$alq$1@dont-email.me>
In reply to#10219
On 7/26/2013 1:04 PM, Luuk wrote:
> On 26-07-2013 04:56, Scott Johnson wrote:
>> I have a situation which I 'think' I need to use variable variables and
>> not sure how to go about it.
>>
>>
>> I have several <select multiple> elements on a search form with their
>> names created dynamically and the options created dynamically.
>>
>> $parent holds the name such as "Women", "Mens" etc..
>> $c_id holds category_id and is sent off as the 'value' of the option.
>>
>> This form is self calling and an array is created for the multiple
>> option selected.
>>
>> Women => array([0]=>20,[1]=>21,...)
>> Mens => array([0]=>32,[1]=>35,...)
>>
>> I am using in_array() to check returned posted values against an id.
>>
>> How do I reference that array by using the value of $parent.
>>
>> if $parent is 'Women' I need to reference the array 'Women'
>>
>> Here is a nutshell of the algorithm.
>>
>> foreach($parent_ary as $parent) {
>>      echo "<select {$parent}[] multiple>";
>>      foreach($category_ary as $c_id=>$category) {
>>          if(in_array($c_id, array of posted value)) {
>>              // Create select option
>>          } else {
>>              // Create regular option
>>          }
>>      }
>>      echo "</select>";
>> }
>>
>> I hope this makes sense and yes I did RTM but could not transpose what
>> they where explaining to what I need to do.
>>
>> Thanks
>> Scotty
>
> This will return 'test':
> $a="test"; $b="a"; print ${$b};
>
> so, if $parent="Woman", then ${$parent} should refer to the $Woman[],
> and if $parent="Mens" then ${$parent} should refer to $Mens[]
>
>
>
>
>

Ahhhh.

I saw that in the manual but could not wrap my head around it.  Thanks 
very much.

Scotty

[toc] | [prev] | [next] | [standalone]


#10220

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-07-26 22:17 +0200
Message-ID<51f2d91e$0$6553$9b4e6d93@newsspool4.arcor-online.net>
In reply to#10212
Scott Johnson wrote:

> if $parent is 'Women' I need to reference the array 'Women'

The concept of variable variables is quite simple:

  $women = array(/* ... */);
  $parent = 'women';
  $$parent == $women; // TRUE

I don't consider this concept "sucky" per se, but I would recommend to
avoid overusing it, as it easily leads to unwieldy code due to its
dynamic nature:

  $a = 1;
  $b = 'a';
  $c = 'b';
  $$$c; // 1
  $c = 'a';
  $$$c; // Notice: Undefined variable: 1

However, sometimes it comes in quite handy, for instance:

  foreach (array('foo', 'bar', 'baz') as $var) {
    $$var = $_POST[$var];
  }

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#10224

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-07-27 09:37 +0200
Message-ID<1769805.qdRcx4SL1G@PointedEars.de>
In reply to#10220
Christoph Michael Becker wrote:

> Scott Johnson wrote:
>> if $parent is 'Women' I need to reference the array 'Women'
> 
> The concept of variable variables is quite simple:
> 
>   $women = array(/* ... */);
>   $parent = 'women';
>   $$parent == $women; // TRUE
> 
> I don't consider this concept "sucky" per se, but I would recommend to
> avoid overusing it, as it easily leads to unwieldy code due to its
> dynamic nature:

ACK.

>   $a = 1;
>   $b = 'a';
>   $c = 'b';
>   $$$c; // 1
>   $c = 'a';
>   $$$c; // Notice: Undefined variable: 1

Thanks, I did not know that you could have variables named β€œ1” this way :)

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $1 . PHP_EOL;'
PHP Parse error:  syntax error, unexpected '1' (T_LNUMBER), expecting 
variable (T_VARIABLE) or '$' in Command line code on line 1

$ php -r '$a = 1; $b = "a"; $c = "b"; $$$c = 42; echo $$$c . PHP_EOL;'
42

Another interesting fact that I have learned (or have been reminded of) in 
the ZCE (PHP 5.3) preparation class recently:

$ php -r '$a = 42; $b = "a";

  echo $$b     . PHP_EOL;
  echo "$$b"   . PHP_EOL;
  echo "${$b}" . PHP_EOL;
  echo "{$$b}" . PHP_EOL;'
42
$a
42
42


PointedEars
-- 
When all you know is jQuery, every problem looks $(olvable).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web