i have a variables like $srange0 , $srange1, $srange2 $srange3.
i am using to declare some value to each value using for loop.
for($i=0;$i<=3;$i++){
$srange.$i = $i;
}
but its not working ?
is there any alternative solution for this
for($i=0;$i<=3;$i++){
$var = 'srange'.$i;
$$var = $i;
}
But, whenever I see variables like that, I'd use an array instead.
Use an array:
$srange = array();
for ($i = 0; $i <= 3; ++$i)
$srange[$i] = $i;
For the purpose of this particular task, you can also do this:
$srange = range(0, 3);
That also builds the same array as my first code snippet.
The properway to add these dynamic variables will be like this
for($i=0;$i<=3;$i++){
$name = 'srange'.$i;
$$name = $i;
}
This may be helpful to you:
$srange0;
$srange1;
$srange2;
for($i=0;$i<=3;$i++) {
$range = "srange".$i;
$$range = $i;
}
echo $srange2."<br />";
exit;
Related
I am trying to create a dynamic variable. I have a loop and I want it to loop through the records and create a variable for each record. My code:
$ct = 1;
foreach ($record as $rec){
$var.$ct = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
When I try to use the above code, it gives me an error saying the $var1 variable doesn't exist/undefined? Is it possible in PHP to create dynamic variables like the above example. If so, what am I doing wrong?
You're looking for variable variables.
Create the variable name as a string, and then assign it:
$ct = 1;
foreach( $record as $rec )
{
$name = 'var'.$ct;
$$name = $rec['Name'];
$ct++;
}
echo $var1;
It would be much better to create an array, though:
$names = [ ];
foreach( $record as $rec )
{
$names[] = $rec['Name'];
}
echo $names[0];
You can use different syntax with {}
$ct = 1;
foreach ($record as $rec){
${'var' . $ct++} = $rec['Name'];
}
echo $var1;
Although isn't it better just to use an array?
Working fiddle
You can with a double $.
$var = "variable";
$$var = "test";
echo $variable;
//echoes "test"
in your example:
$ct = 1;
foreach ($record as $rec){
$varname = "var" . $ct;
$$varname = $rec['Name'];
$ct = $ct + 1;
}
echo $var1;
Please try this, let me know if it works for you.
I use a prefix with the dynamic variable.
$ct = 1;
$prefix = 'var';
foreach ($record as $key=>$rec){
$temp = $ct;
$ct = $prefix.$ct;
$$ct = $rec;
$ct = $temp + 1;
}
echo $var1;
You can do that using array easily. But if you really want it to be in dyanamic vairable individually, in that case also , I would like to suggest you to get help in array way. This way, you can track you variables.
In the below mentioned way, you event don't need to take a extra variable like $ct. Just an array $var and applying extract method on it after the loop will do the trick.
$var = [];
foreach( $record as $k => $rec )
{
$var['var'.$k] = $rec['Name'];
}
extract($var);
echo $var0; //or $var_1 or $var_2
I am in too much trouble. I need below type of array:-
$val = "abc";
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
I tried below:-
for($i = 1; $i<16; $i++)
{
$arr.$i["besk"] = $val
}
here I've $val. so not to worry on that. But array is not properly creating. Any help would be appreciated.
first define the array as string
like
$arr = 'arr';
then use the foreach
like
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
You need to use variable variables (not recommended)
for($i = 1; $i<16; $i++)
{
${"arr".$i}["besk"] = $val
}
EDIT : #CBroe is right about his comment, you should use an array instead. So the best solution would be to create a two dimensional array like so :
$arr = [];
for($i = 0; $i<15; $i++)
{
$arr[$i]["besk"] = $val
}
The only difference is your array indexes start from 0 now and if you want to have the third value of your array you need this command $arr[2]["besk"]
it is very simple use this:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val
}
Use this approach:
for($i = 1; $i<16; $i++)
{
${$arr.$i}["besk"] = $val;
}
add new variable
$val = "abc";
$arrName = "arr"; //this one
$arr1["besk"] = $val
$arr2["besk"] = $val
.
.
$arr15["besk"] = $val
and to call it
for($i = 1; $i<16; $i++)
{
${$arrName.$i}["besk"] = $val
}
ps. you did not create array, you just create 15 array variable with 1 index("besk" index)
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();
}
How to create array from variable php using loop for ?
i have many variable php like
$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;
i want to create array like this
$ar = array('1','2','5','2','6');
but using loop for like
for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
Not a recomended way of doing things but:
$arr = array();
for($i=0;$i<5;$i++) {
$varName = 'number_'.$i;
$arr[] = $$varName;
}
for ($i = 1; $i <= 6; $i++)
$ar[] = $i;
for ($i=1;$i<7;$i++)
{
$ar[] = $i;
}
I need to create a php file with a hundred variables, which are all identical except for their id.
PHP Code
$myvar1 = get_input('myvar1');
$myvar2 = get_input('myvar2');
$myvar3 = get_input('myvar3');
$myvar4 = get_input('myvar4');
...
$myvar100 = get_input('myvar100');
I wonder if it is possible to create only one line as a model, and is replicated 100 times?
Thanks.
Just store it in an array instead of 100 variables:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i] = get_input('myvar' . $i);
}
Or if you want the indexes to start at zero:
$myvar = Array();
for ($i = 1; $i <= 100; ++$i) {
$myvar[$i - 1] = get_input('myvar' . $i);
}
You really must use Arrays for this, its far more appropriate:
$myvar = Array();
for($i=1;$i<101;$i++) $myvar[$i]=get_input("myvar{$i}"); // $myvar[1]=... etc...
Anyway, its possible to create variable names dynamically (notice the '$$'):
for($i=1;$i<101;$i++){
$varName="myvar{$i}";
$$varName=get_input($varName);
}
you could store your variables in an array
$myvarArr = array();
for($i=0;i<100;i++) {
$myvarArr[$i] = get_input('myvar' . ($i+1));
}
Looks like you just need an array, an array right from the outside variable.
if you make your form field name like this
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
<input type="text" name="myvar[]">
you can easily get all the values by as simple code as
$myvar = $_POST['myvar'];
Just to show, that it's possible, use variable variables (i just love this php feature):
for ($i = 1; $i <= 100; $i++) {
$varName = "myvar{$i}"; // create variable name
$$varName = get_input("myvar{$i}"); // $$varName => $myvar5, when $i == 5 and so on
}