I want to access a variable that is either called $item1, $item2 or $item3.
I want to access this variable inside a for loop where $i is ++ every time. using $item.$i or something similar. However using that code means that I am trying to join the contents of two variables, and there is no variable called $item.
Arrays: A Better Method
While PHP does permit you to build dynamic variable names from various other values, you probably shouldn’t in this case. It seems to me that an array would be more appropriate for you:
You could then access each value individually:
Or loop over the entire set:
Merging Multiple Arrays
You indicated in the comments on the OP that
$item1through$item3are already arrays. You could merge them all together into one array if you like witharray_merge(), demonstrated below:Which outputs:
If You Must: Dynamic Variable Names
For completeness, if you were to solve your problem by dynamically constructing variable names, you could do the following: