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 < count($day); ++$l) {
for ($k = 0; $k < count($B); ++$k) {
$days = $day[$l];
$week = $B[$k];
$arr[$days . $week] = $_POST["'" . $days.$week . "'"];
};
};
Which would generate:
Array
(
[MonP1B] =>
[MonP2B] =>
[MonP3B] =>
...
[FriP4B] =>
[FriP5B] =>
[FriP6B] =>
)
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 < $number_of_days; ++$day) {
for ($room = 0; $room < $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 callsCached thecount()
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.