Q:

Most efficient php boolean

I'm quite new to php so I'm wondering what the most efficient way is to do this. For exmaple I have 3 different colors for 3 cars as variables. So car 1 gets color 1, car 2 color 2 and car 3 color 3. A car only gets a color if it has none already. What is the most efficient way to do this?
Something like this?
    if (is_car('1') AND !hascolor) {
        give_color_1 }
    else {
        echo 'Car already has a color'
    }

And then this boolean also for 2 and 3? Or is there a way to make this shorter? Thanks!

A:

One way to do it is using arrays. So for your example:
// initialize the array
$car_color_array = Array('1' => '', '2' => '', '3' => '');
$color_array = Array('1' => 'color1', '2' => 'color2', '3' => 'color3');

foreach ($car_color_array  as $car => $color){
    // if color is not set
    if ($color == ''){
        $car_color_array[$car] = $color_array[$car];
    }
}

// this should give cars with colors now
foreach ($car_color_array  as $car => $color){
    echo "<BR>Car: " . $car . " -> Color: " . $color;
}

