I need some variables in the following format:
$m_01;
$m_02;
$m_03;
.....
.....
.....
$m_12;
The digits in the variables are the months of the calendar.
I can declare 12 variables separately. However, I want to declare the variable using a loop. So I did something like this.
for($i = 1; $i <= 12; $i++) {
if($i<10)
$month = '0'.$i;
else
$month = $i;
$m_$i;
}
However, I am getting some error:-
Parse error: syntax error, unexpected '$month' (T_VARIABLE) in /var/www/html/custom/ci/Dotell_customer/application/controllers/login.php on line 412
How can I overcome this issue?
NOTE:
$$month creates a variable 01;
Is there any way where I can cave variable m_01?
P.S. I am aware of array. I just want to learn PHP variables variables.
This is not the best practise to follow, but still just for the answer:
for($i = 0; $i <= 12; $i++) {
${"m_$i"} = $i;
}
echo $m_1;
echo $m_2;
echo $m_3;
The best practise would be to create an array. For eg:
$arr = [];
for( $i=0; $i <=12; $i++ ) {
$arr['m_'.$i] = $i;
}
print_r($arr);
I think you are looking for something like
$varname = 'm_'.$i; // format the way you need it
$$varname = ...
As noted in the comments, it's rather a code smell, and often unnecessary. Typically an array does a better job, i.e.
$m[$i] = ...
Use PHP variable variables. This works as also suggested in the comments
${"m_$i"}
Going with Davids suggestion of using an array.
$month = [];
for($i = 1; $i <= 12; $i++)
{
if($i<10)
$month[] = '0'.$i;
else
$month[] = $i;
$month[$i];
}
here the $var[] = ... syntax appends your array. the $month[$i] accesses it.
Use of double variable is not usually suggested. You should really use an array to solve your problem as:
$month = array();
for($i = 1; $i <= 12; $i++)
{
if($i<10)
$month['m_0'.$i] = $i;
else
$month['m_'.$i] = $i;
}
otherwise if you really want to necessary go for double variable then :
for($i = 1; $i <= 12; $i++)
{
if($i<10)
${"m_0$i"} = $i;
else
${"m_$i"} = $i;
}
I hope it helps
for($i = 1; $i <= 12; $i++){
if($i<10)
$month = '0'.$i;
else
$month = $i;
$month = 'm_' . $month;
..........
}
Access var as
assignment $month = 'm_12';
value $$month
Related
I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";
I was wondering if it is possible to create unique variables in a for loop using PHP. I tried the following code but it didn't work:
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$level_ + $i = array();
}
I would like to end up with the variables $level_1, $level_2, $level_3, $level_4, $level_5 and $level_6. How would I achieve this?
$level_count = 6
for ($i=1; $i<=$level_count; $i++) {
$l = "level" . $i;
$$l = array();
}
But Zerkms is right...
$arr = array(array(),array(),array(),array(),array(),array());
It's much easier if you use arrays for this. Try this one-liner:
$level_count = 6;
$array = array_fill_keys(array_map(function($index) {
return 'level_' . $index;
}, range(1, $level_count)), array());
var_dump($array);
Weird thing (I have no idea why you want to use it), but, just for educational purposes...
$level_count = 6;
for ($i = 1; $i <= $level_count; $i++) {
$name = 'level_' . $i;
$$name = array();
}
I'm working on another developers old code and there are tons of Notice: Undefined index errors when data is being set using the += operator. Essentially the index is not set yet in the array so the error is generated.
Example:
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
$myArray['test'] += 1;
}
Will generate an error on the first run since the test index is not set yet.
I know I can remove this error with the following code:
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
if ( ! isset($myArray['test']) )
{
$myArray['test'] = $myValue;
}
else
{
$myArray['test'] += $myValue;
}
}
However, there are around 50 of things like this to change. Is it worth writing all these isset statements or is there a better way I am not aware of?
EDIT: I should note that the array indexes aren't always the same and sometimes aren't set so I can't preset the index in this circumstance to avoid the error.
This is a bit shorter, but perhaps still a bit complicated if you have many edits.
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
isset($myArray['test']) ? $myArray['test'] += $myValue : $myArray['test'] = $myValue;
}
You could also write a global function (not tested)..
$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
increment($myArray['test'], $myValue);
}
function increment(&$var, $inc){
$var = isset($var) ? $var += $inc : $var = $inc
}
If you are using PHP 7 and above, you can use the Null Coalescing Operator to make this code cleaner.
$myArray = [];
$myValue = 1;
for ($i = 1; $i <= 10; $i++)
{
$myArray['test'] = $myValue + ($myArray['test'] ?? 0);
}
The benefit here is not only that the code is cleaner, but you're also being more explicit about the default value (0)
Old/Deprecaded/Unrecommended but the shortest solution is
#$myArray['test'] += $myValue;
I need a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?
Hi I have a list of values named:
$value1
$value2
$value3
...
and I'd like to assign each value to an array element; something like:
$my_array[1]=$value1;
$my_array[2]=$value2;
$my_array[3]=$value3;
How can I do this using a for cycle? The array is not a problem but I can't figure out how to write some code for the value, it should be something like:
for($i=1; $i<=10000; $i++)
{
$my_array[$i]=$value$i;
}
Try this:
for ($i=1; $i<=10000; $i++) {
$val_name = "value" . $i;
$my_array[$i]=$$val_name;
}
You are almost there:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value;
}
Or this, if you want to append the counter as well:
for($i=1; $i<=10000; $i++)
{
$my_array[$i] = $value . $i;
}
What you are looking for are the {}.
$my_array[$i]=${'value'.$i};
for ($i = 1; isset(${"value$i"}); $i++) {
$my_array[$i] = ${"value$i"};
}
This syntax is known as variable variables.
You can use the $$ syntax:
for($i = 1; $i <= 10000; $i++) {
$name = 'value' . $i;
$my_array[$i] = $$name;
}