All Questions
Tagged with php variable-variables
157 questions
3
votes
1
answer
139
views
How to convert a string in an array to variable using the array index in PHP [duplicate]
Let's say I have an array_1 containing a string which is a name of another array_2. And now I would like to convert the array index into a variable containing the array_2.
I have the following working ...
0
votes
2
answers
94
views
In PHP, how to recurse through a Closure when the variable name holding the anonymous function is a variable-variable
I have a list of about 100 articles, and within each article is a list of clauses. The clauses are a list of varying levels deep.
$clauses = array(
[
'Fields' => ['Clause' => 'clause 1', '...
1
vote
2
answers
193
views
Variable variable is not accessible within callback scope while using arrow function syntax (Warning: Undefined variable)
I need to use a variable variable for defining a lambda function inside of the callback of array_map(). I get an "Undefined variable" WARNING for the variable that I am referencing. ...
0
votes
0
answers
64
views
How can I echo a php variable that's INSIDE a php string variable?
I've got a database full of text fields, all of which contain WITHIN them the characters $myvariable.
When I retrieve such text field and save it as a php variable, say as $myDBtext, I can definitely ...
0
votes
2
answers
319
views
PHP variable variable not overriding original variable
I am trying to override some variables in my PHP file using variable variables, but I am getting unexpected results, where the original value is never overwritten, but where the new variable-variable ...
1
vote
3
answers
389
views
Variable variables as parameters of custom function in usort()
I've been playing around and seeing if I can set up a more dynamic method in PHP.
usort(
$dataset,
function($a, $b){
return strcasecmp($a[$this->parameters], $b[$this->parameters]...
2
votes
1
answer
704
views
Trying to access $_SERVER(or any global) variable from string name [duplicate]
Today I met such a terrible situation.
It seems this bug is related to PHP.
I'm trying to access to $_SERVER or another super global variables but from string name.
This version of implementation is ...
2
votes
1
answer
112
views
Variable Variables with array element values syntax
I want to create a session variable but the session variable name I want to be dynamic. So what I need is the proper syntax for a variable name that is a $_SESSION variable.
I have tried the code ...
1
vote
1
answer
92
views
How can I access a specific depth of an associative array with variable variables
I've been hitting my head on this problem for some time now. I am working on a piece of software that creates a tree from a MySQL result checking for changes between each row to decide where in the ...
-1
votes
1
answer
74
views
Extracting json data using variable variables?
I have a .json file with some data I'm trying to extract. What I need to know is how I would go about making the below variable dynamic.
$MapData['MapData']['0,0']['type'];
What I need to know is how ...
0
votes
1
answer
109
views
Passing variable variable into closure throws a parse error?
I just noticed that if you try to put a variable variable into a use list of a closure, it throws a parse error. Example code:
foreach ($array as $key => $item) {
$$key = $item->something;
...
-2
votes
5
answers
1k
views
Use list of variables as arguments when defining php function
I am trying to use a list of variables as arguments when DEFINING a function. It seems like it should be straight forward, but no. The callback is easy and I am able to use (...$listOfVariables) to ...
-1
votes
1
answer
71
views
How to for loop 10 times values in single string using comma?
How I can use for loop PHP to create same result in $d4_c_d_v as below?
Note: $d4_c_d_v must end without comma at the end.
$d4_c_d_v =
$g_demographics_value_0['id'] .','.
$g_demographics_value_1['...
1
vote
0
answers
379
views
Undefined variable on defining variable variables
I want to import data from a .json file into a PHP file and dynamically, by using variable variables and a foreach loop, define new variables. However, PhpStorm doesn't recognize my variables and give ...
0
votes
2
answers
152
views
How can I make a variable's name from another variable AND string?
Say I had the variable $foo which has a value of "bar" and I wanted to make a variable variable from it, but also append the string "123" to it, so that the variable name is $bar123. How would I do ...
-3
votes
2
answers
579
views
Using str_replace in variable variable in php
I have a suitecrm implementation where I would like to print a numbers in words in an invoice. The number is currency formatted, so it comes with commas (,). While I try to replace the commas with ...
3
votes
2
answers
412
views
Using eval() in the code, and how would you avoid it?
In my code, I want to try fetching $_POST or $_GET variables, and unsetting them as soon as they were requested once.
This function returns the method used, which is simple enough. Unfortunately I ...
3
votes
2
answers
48
views
Variable properties on a variable method
I'm using a third-party package which manipulates images. For basic conversions it accepts a single value:
$image = $this->addMediaConversion('thumb');
$image->width(100);
$image->height(...
-3
votes
2
answers
81
views
PHP: How to increase a number in variable that contains both text and the number [duplicate]
This is quite a stupid question. I want to find out how to increase a number in the end of the variable:
$Coffee1 = "black";
$Coffee2 = "brown";
$Coffee3 = "gray";
echo $Coffee1; => black
...
0
votes
0
answers
113
views
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 ...
1
vote
1
answer
66
views
Are different PHP variable variables that resolve to the same variable name the same variable?
Let's say I have an array of strings that I use to create variable variables:
$var_arr = ["item1", "item2", "item3"];
foreach ($var_arr as $item) {
$$item = array();
}
Then elsewhere in my code I ...
0
votes
1
answer
97
views
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 ...
0
votes
2
answers
1k
views
auto add number at end of variable
i have this variable
$n= "";
it can be 1 or 2 example: $n= "1"; or $n= "2";
so i want $number to be changed to $number1 or $number2 so it will get 1 or 2 from $n
$live = ($number1-$r3) / 60;
...
0
votes
1
answer
108
views
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){
...
0
votes
3
answers
2k
views
php: How to get element in array by index using variable-variables
If I have:
<?php
$params=[
[
'string'
]
];
//params get from $this->params()->fromPost();
$abc='params[0][0]';//$abc dynamiccly
I cannot access $params[0]
How to get string ...
0
votes
1
answer
38
views
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. ...
-1
votes
1
answer
391
views
How to use foreach to loop through a file with variable variable?
The following PHP code using foreach does not seem to work. I believe it has to do with "<a href='/$value/access'>".
I've shared the entire codebase.
Does anyone know what is wrong with my ...
0
votes
1
answer
109
views
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 '...
-1
votes
1
answer
184
views
Class & Object Scope for Variable Variables in PHP
Question 1) Why is the Variable/variable $this->$template not visible to parse_and_return(), lines 42-50, but visible to $this->fetch, lines 456-472. I thought parse_and_return and $this->...
-3
votes
1
answer
2k
views
Using R, how to reference variable variables (or variables variable) a la PHP
Using R stats, I want to access a variable variable scenario similar to PHP double-dollar-sign technique: http://php.net/manual/en/language.variables.variable.php
Specifically, I am looking for a ...
3
votes
1
answer
386
views
Dynamically numbered variables in foreach loop
I have an array full of sub-arrays, which in turn are full of source names and their website URLs, such as The New York Times and http://newyorktimes.com.
I'm displaying a list of links where the ...
1
vote
1
answer
109
views
Use variable variables with PHP's Superglobal arrays outside of function and class
I'm beginner of PHP, I found a Warning says:
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.
So, does it mean I can use variable ...
6
votes
4
answers
3k
views
Variable variables handling order: changes in PHP 7
With the new PHP 7.0.0 out now, I'm a bit worried about the changes in evaluation order of the so-called 'variable variables'.
On this page, under 'Changes to variable handling', a table is displayed ...
0
votes
1
answer
410
views
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'] = ...
2
votes
1
answer
104
views
How to reach variable variables which has spaces? [duplicate]
I was curious about how to reach variable variables which contain spaces or special chars.
Is there a way to do ?
<?php
$first_day = "monday";
$$first_day = "first day of the week";
echo $monday."...
0
votes
1
answer
43
views
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 = '...
1
vote
4
answers
184
views
Using variable variables to populate array results in error
I have the following code:
function filterUsers(array $setOfAllUsers) {
if (empty($setOfAllUsers)) {
return array(array(), array());
}
$activeUsers = array();
$inactiveUsers = ...
2
votes
2
answers
396
views
PHP - Difference in creating variable variables with brackets or double dollar signs?
From what I can tell in the PHP Manual, it doesn't seem like there is much difference between defining a variable variable with double brackets or double dollar signs.
$foo = 'hello';
$$foo = 'hi';
...
2
votes
2
answers
149
views
Variable variable for array
I have defined an array $array[1][1][test]="hello world". I would like to get this value by using variable variables. I have tried this, without success:
$var1="array";
$var2="[1][1]";
$var3="['test']...
0
votes
4
answers
2k
views
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';
...
1
vote
1
answer
206
views
Dynamically append indexes to variable
So I have arrays of different lengths
pathes= array(array('f1e9'),
array('c0d9', '0', 'form_values', '6e13')
);
For each path I would like to use each value as an index to scan a ...
2
votes
1
answer
100
views
Variable variables doesn't work as expected and is creating an array
I'm using this line of code:
$var{++$counter} = $results['row'];
I've set this up with a goal of creating these variables:
$var1 = row 1
$var2 = row 2
$var3 = row 3
Why is it created an array for $...
3
votes
2
answers
237
views
How can I concatenate a string with a variable and use it as another variable?
I have a variable $language, which can be: "en", "nl", or "fr".I have 3 other variables $menu_en, $menu_fr and $menu_fr.
I have a php page where I want the menu to appear in any of the 3 languages.
...
-1
votes
1
answer
32
views
Variable in global variable: "global variable{variable}". It works, but is it OK?
TL;DR: Say I have a global instantiated class
$GLOBALS['my_plugin'] = instance();
and I want to use it somewhere else, but I dont know the name 'my_plugin'. Actually I do of course, but my helper ...
0
votes
1
answer
161
views
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 ...
0
votes
2
answers
306
views
How to use variable variables in class variables and function arguments
This is what I want to do:
class Contacts {
private $_plural = 'contacts';
private $_single = 'contact';
private $_factory = 'contactfactory';
private $_model = 'contact_model';
...
1
vote
1
answer
127
views
Can't pass a variable variable into a function in PHP since upgrading from 5.5.22 to 5.6.6?
This line of code worked perfectly before I upgraded, now it doesn't:
123: $thisRow[] = displayR(${"sR{$i}S1"}, ${"nR{$i}S1P"});
I get this error:
Undefined variable: sR1S1 in script.php on line 123
...
0
votes
1
answer
111
views
Access a variable using a string value as it's name [duplicate]
I have this php code foreach loop inside another foreach what I need is to set all the array value one by one on each loop(red, blue, green, black). I need to change $green in
data-percent="<?...
1
vote
1
answer
329
views
JavaScript variable variables
Say I have object:
function obj()
{
this.prop1;
this.prop2;
this.prop3;
}
and an array of obj's
objects = [new obj(),new obj(),new obj()];
I want to easily iterate through each using jquery ...
0
votes
2
answers
638
views
PHP Variable Variable Request Method
I'm working on a web library for personal use. I thought I'd be all clever and use variable variables to make my library support all request methods easily.
I had:
$request = '_' . $_SERVER['...