Calling a variable from string in PHP - php

i need to call similar variable on shortest way.
$test = new stdClass();
$test->var0=0;
$test->var1=1;
$test->var2=2;
And now i need to echo all 3 variables in for cycle, not like this:
echo $test->var0;
echo $test->var1;
echo $test->var2;

Try this -
for($i = 0; $i < 3; $i++) {
echo $test->{'var' . $i};
}

Related

How to create string from string in php for loop with foreach

How I can create string (like I tried in modules_for) to get foreach from $g_module_1?
Current echo saying: _1 and _2
Should be: apple_1 and banana_2
$g_module_1 = 'apple';
$g_module_2 = 'banana';
for ($i = 1; $i <= 2; $i++) {
$modules_for = $g_module.'_'.$i;
foreach ((array)$modules_for as $m_foreach_as) {
echo $modules_for;
}
}
You are searching for Variable variables.
In your case it would be echo $$modules_for; - with another fix before: $modules_for = 'g_module_'.$i;
So the whole code would be
<?php
$g_module_1 = 'apple';
$g_module_2 = 'banana';
for ($i = 1; $i <= 2; $i++) {
$modules_for = 'g_module_'.$i;
//foreach ((array)$modules_for as $m_foreach_as) {
echo $$modules_for;
//}
}
I'm not sure what your intention with the foreach was, it doesn't really make sense here, so I commented it out..
A fiddle can be found here: https://3v4l.org/nsYq2
Sidenote: This looks like you could be better off with an array.

Declaring a variable dynamically in PHP

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

PHP variables in a complex way

<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}

php variable after a variable in for loop

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?

PHP loop in object

I have 4 instances of the same object:
echo $payment->field_course_1_length[0]['value'];
echo $payment->field_course_2_length[0]['value'];
echo $payment->field_course_3_length[0]['value'];
echo $payment->field_course_4_length[0]['value'];
My question is so that I don't have to be typing the same over and over can I put it in a loop like this:
for ($i=1; $i<5; $i++) {
echo $payment->field_course_'$i'_length[0]['value'];
}
Thank you.
echo $payment->{"field_course_{$i}_length"}[0]['value'];
$tmp = $payment->{'field_course_' . $i . '_length'};
echo $tmp[0]['value'];
However, I strongly recommend to use arrays instead of dynamic properties. If they are not dynamic, don't access them dynamic.
You can do like this:
for($i = 1; $i <= 4; $i++) {
echo $payment->{"field_course_".$i."_length"}[0]['value'];
}

Categories