Concatenating 2 array variable names to create a new variable name I'm developing a room booking system using PHP for a school project.

I'm trying to create a variable name by concatenating values from two arrays together. 
here are the two arrays:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');


and then here is the code I'm currently using to try concatenate them:

// getting all the values from the table using $_POST. There are 120 different values so it is best to create these variables using a procedure

for ($l = 0; $l &lt; count($day); ++$l) {
    for ($k = 0; $k &lt; count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        ${$days . $week} = $_POST["'" . $days.$week . "'"];
    };
};


Is the syntax of the concatenated variable correct? Is there a better way of doing this?

The final variable names of the concatenated variable should look something like:

$MonP1B
$MonP2B
$MonP3B
$MonP4B
$MonP5B
$MonP6B
$TueP1B 


etc.
I have to agree with bumperbox here - why create separate variables instead of using an array to store them all, as follows:

$day = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$B = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$arr = array();

for ($l = 0; $l &lt; count($day); ++$l) {
    for ($k = 0; $k &lt; count($B); ++$k) {
        $days = $day[$l];
        $week = $B[$k];
        $arr[$days . $week] = $_POST["'" . $days.$week . "'"];
    }; 
};


Which would generate:

Array
(
    [MonP1B] =&gt; 
    [MonP2B] =&gt; 
    [MonP3B] =&gt; 
    ...
    [FriP4B] =&gt; 
    [FriP5B] =&gt; 
    [FriP6B] =&gt; 
)


And which you can access via $arr['FriP5B'] for example. 

Now, as to how you can improve your code, see this:

$days_of_the_week = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$number_of_days = count($days_of_the_week);
$rooms = array('P1B', 'P2B', 'P3B', 'P4B', 'P5B', 'P6B');
$number_of_rooms = count($rooms);
$booked_day_room = array();

for ($day = 0; $day &lt; $number_of_days; ++$day) {
    for ($room = 0; $room &lt; $number_of_rooms; ++$room) {
        $days_room = $days_of_the_week[$day] . $rooms[$room];
        $booked_day_room[$days_room] = isset($_POST[$days_room]) ?: $_POST[$days_room];
    };
};

print_r($booked_day_room);


What we did:


Used descriptive variable names
Cached the count() function calls
Stored everything in 1 array, so we don't have to chase 100 different variables


Why are we using an array over multiple variables? Multiple variables might be a bit faster, and they might use more or less the same memory, however from a code readability standpoint, you want one array as it is easier to keep track of in the code.