Create new variables in a loop (PHP)? [duplicate] - php

This question already has answers here:
PHP - How Can I Create Variable Names Within a Loop?
(6 answers)
How do I dynamically create the variable name in a PHP loop?
(1 answer)
how to change php variable name in a loop?
(5 answers)
Declare variables on the fly using for loop
(5 answers)
Create new variables with foreach loop
(3 answers)
Closed 4 years ago.
I have a problem with PHP. I'd like to create a new variable each time, my loop runs through. For example: If my loop runs for the first time, it should create a new variable called $loop1, if it runs the second time $loop2 with a different value, third loop - $loop3...
Here is a reference of my code, any help is appreciated!
while ($x < count($lines)) {
$info
x++;
}

You can create variables dynamically using braces in PHP, e.g.:
${"loop" . $x} = "some value";
(Reference: PHP "variable variables")
But note that this might be a bad idea in real-world application.
It might be more appropriate to use an array or a map instead, e.g.
$loopVars = []; // create a new array
while (...) {
$loopVars[] = "some value"; // add a new array element
}

I guess you are searching for something like an array.
$data = array();
while ($x < count($lines))
{
$data[] = ... whatever you want to store
}

Related

how to define array in php like array1[] array2[] using for loop [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 4 years ago.
How to define array in php like array1[] array2[] using for loop like instead of a1 or a2 I can write ai and its in for loop is it possible?
Like
for($i=0;$i<10;$i++)
{
$ai[$i]="hello'";
}
so I would like to say as like
a1[1]=Hello
You can access variables dynamically using this:
${"a$i"}[$i] = 'hello';
You could use a dynamic var
$ai.$i[$i] = 'Hello';
or
$ai.$i[$your_index] = 'Hello';
but be careful when doing this .. in this case it is difficult to keep in control of the variables .. is easy to make a mistake

Why to declare array before setting it (php)? [duplicate]

This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)
You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

Is there a way to get just one element from array that is returned as a function? [duplicate]

This question already has answers here:
Accessing an array element when returning from a function
(3 answers)
Closed 9 years ago.
Say I have a function/method that returns an array, let's call it ArrayReturner(). But I only want the first element, [0]. Right now I'm doing something like...
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
Is there a way to do that in one line without the need for the temporary $arrayReturned array?
Try:
$arrayReturned = reset(ArrayReturner());
Depends on PHP's version you use.
If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.
If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Categories