1
\$\begingroup\$

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 < count($day); ++$l) {
    for ($k = 0; $k < 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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ This is a really bad idea, storing each value as a variable will make it really difficult to use. You are far better off storing them in array like it already is in the $_POST array, or better yet a multi-dimensional array so you can reference a day at a time. \$\endgroup\$
    – bumperbox
    Commented Nov 17, 2014 at 19:31
  • \$\begingroup\$ Also consider using an off-the-shelf software package instead of writing your own. \$\endgroup\$ Commented Nov 17, 2014 at 20:26
  • \$\begingroup\$ @bumperbox Sounds like a better idea than the original (storing in a multi-d array). Off-the-shelf is something I cannot use as the project is one for coursework and requires me to code a system myself :) thank you Bumperbox! \$\endgroup\$
    – Yash Morar
    Commented Nov 17, 2014 at 21:54

1 Answer 1

2
\$\begingroup\$

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:

  1. Used descriptive variable names
  2. Cached the count() function calls
  3. 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.

\$\endgroup\$
2
  • \$\begingroup\$ You might want to add isset around $_POST in-case there are non-existent keys \$\endgroup\$
    – bumperbox
    Commented Nov 17, 2014 at 20:10
  • \$\begingroup\$ Good call, adding it in \$\endgroup\$
    – jsanc623
    Commented Nov 17, 2014 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.