i have the following 3 variables:
private $a_total;
private $b_total;
private $c_total;
now when the fields are filled out with some data for totals, names of those fields are “a”, “b”, and “c”. I want to store it dynamically to the variables above something like:
$type = $_POST['totaltype']; //either a, b or c
$to_save = "{$type}_total";
$this->$to_save['total'] = some number;
if i try to
print_r($this->$to_save);
it gives empty array. If i try to:
print_r($this->$to_save['total']);
it gives correct number.
can anyone help?
Note: i want to use dynamically because these data will be inside a big loop so i don’t want to reuse $a_total, $b_total, $c_total since i will have more than a, b and c variables.
You can wrap the variable name in curly-brackets inside the class:
The same works when accessing it like an array:
You can also save the full-name of the variable in a string, such as your
$to_saveand access it the same way: