Questions tagged [variable-variables]

Language feature of PHP: A variable variable takes the value of a variable and treats that as the name of a variable.

Language feature of PHP: A variable variable takes the value of a variable and treats that as the name of a variable.

171 questions
0
votes
0 answers

How do you set a static variable variable in object?

I have an object Obj with a method sql() that creates SQL queries to update a database. The tables in the database have unique names for their ID fields. My objective is to be able to define the table name and ID field name in the child…
tj12
  • 1
0
votes
2 answers

How do I split and reconstruct a variable name while holding its original value

Is it possible to split variables that have already been assigned values, and re-piece them back together to hold those same previous values? For Example: URLs.QA.Signin = 'https://qa.test.com' TestEnvironment = 'QA' CurrentURL = 'URLs.' +…
bitShredder
  • 132
  • 1
  • 11
0
votes
1 answer

"Variable variable" syntax

This is a question related to getting Drupal CCK fields (just in case that happens to change anything). I have several Drupal CCK fields with similar names. They have the same name with a number at the end. that I'd like to pull values from these…
Kerri
  • 1,211
  • 2
  • 15
  • 22
0
votes
1 answer

Calling Class property using variables in Zend

I am creating partials where an items property names are passed from an array to access the values of those properties but I receive an error saying Undefined property of Namespace\Entity::$variable when defining $property->{$variable}. How would I…
0
votes
2 answers

Javascript dynamically create variables from array

just started to dabble with Javascript so I'm sorry if this is a basic question. I have an array of keys: keys = ['fruits','vegetables','fats','meats'] I want to dynamically create a Map for each of these elements in the array (the length of this…
0
votes
1 answer

Is there any case in which PHP's variable variables make a solution clearer?

Possible Duplicate: what's an actual use of variable variables? Is there any case in which PHP's variable variables make a solution clearer? E.g. this: $a = 'hello'; $$a = 'hello, world!'; echo $hello;
Joe D
  • 2,855
  • 2
  • 31
  • 25
0
votes
1 answer

Howto make a var. name with a var. in C/C++?

I have some vars live: int foo1; int foo2; .. and I want to reach them from: for (int i = 1;i<=2;i++) { // howto get foo1 and foo2? } how to get them? EDIT, end what when it will be no int but a Opject *pointer;?
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
0
votes
1 answer

Dynamic class creation in php

I'm trying to do a bit of php magic in one line with instantiating a class, but it seems the parser wont allow me. Example: class Test{ private $foo; public function __construct($value){ $this->foo = $value; } public…
Dennis
  • 909
  • 4
  • 13
  • 30
0
votes
3 answers

php: How to get element in array by index using variable-variables

If I have: params()->fromPost(); $abc='params[0][0]';//$abc dynamiccly I cannot access $params[0] How to get string element? I try to use echo $$abc; But Notice: Undefined…
0
votes
1 answer

PHP - returning a variable variable from a function scope

Is there a way to return a variable variable from a function? This is what I tried: function varvar($num){ $var = "foo".$num; return $$var; } varvar(3); echo $foo3; But nothing prints out. Any ideas?
adamdesign
  • 190
  • 1
  • 18
0
votes
1 answer

PHP and variable variables ($$) syntax

Before upgrading to PHP 7, I had this code and it returned true var_dump(isset($$_SESSION['payment']) ); var_dump(is_object($$_SESSION['payment'])); var_dump($_SESSION['payment']); // string 'moneyorder' After upgrading to PHP 7, I rewrote the…
kurama
  • 677
  • 3
  • 8
  • 16
0
votes
1 answer

Using a variable variable in an object property in PHP

I've set a few variables: $field = "XYZ"; $block_hi = $field."_hi"; $block_lo = $field."_lo"; Then I have an object with properties that have the name of my above variables: $obj->XYZ_hi['val'] = "value1"; $obj->XYZ_lo['val'] = "value2"; I thought…
0
votes
1 answer

php variable variables to access property of an object

I want to store an property->object name in a table and access in code. What is the proper way to accomplish the following. $patient = new stdClass(); $patient->patient_number = '12345'; $cls = 'patient'; $prp = 'patient_number'; echo 'test1 = '…
0
votes
4 answers

PHP variable not working when referenced with '$'

I'm trying to convert a PHP variable to a JS variable using a little helper function that uses variable variables. To simplify, here is what I'm trying to accomplish: $project_key = 'project 1'; function to_js($variable) { echo $$variable; …
northamerican
  • 2,185
  • 1
  • 20
  • 31
0
votes
1 answer

Potential security issues with variable variables

I often use the following lines to simplify handling form input. foreach($_POST as $key => $value){ $$key = $value; } This is really handy because you only need there lines and you've got all of the things you submitted as variables, but every…
pocketg99
  • 124
  • 1
  • 12