Displaying recent buyers' feedback on games I have the following code made to output the following:



Is there an efficient way to simplify it and not have to repeat the same if(!empty($st[$i]['feedback'])) { code block 2 times just to seperate 2 feedback lines with a echo '&lt;/div&gt;&lt;br&gt;&lt;div class="row center"&gt;';?

&lt;?php

//Get 4 Feedback from Database;
$st = fetch("SELECT username,feedback,feedback_type,feedback_date FROM purchases WHERE feedback IS NOT NULL AND game = :game AND platform = :platform ORDER BY feedback_date DESC LIMIT 4","unfed_payments",array(":game"=&gt;$urlname,":platform"=&gt;$platform));

echo '&lt;div class="row center"&gt;';

//Output the first 2 Feedback on line 1;
for($i = 0; $i &lt; 2; $i++) {

    if(!empty($st[$i]['feedback'])) {

        echo '
        &lt;div class="col-lg-6 center"&gt;
            &lt;div class="col-sm-12 center" style="width: initial; background-color: ';

            if($st[$i]['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };

            echo '&gt;
                &lt;h2 style="font-size: 30px; color: #fff;"&gt;';
                    echo $st[$i]['feedback'].'
                &lt;/h2&gt;
            &lt;/div&gt;
        &lt;/div&gt;';

    }

}

echo '&lt;/div&gt;&lt;br&gt;&lt;div class="row center"&gt;';

//Output the last 2 Feedback on line 2;
for($i = 2; $i &lt; 4; $i++) {

    if(!empty($st[$i]['feedback'])) {

        echo '
        &lt;div class="col-lg-6 center"&gt;
            &lt;div class="col-sm-12 center" style="width: initial; background-color: ';

            if($st[$i]['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };

            echo '&gt;
                &lt;h2 style="font-size: 30px; color: #fff;"&gt;';
                    echo $st[$i]['feedback'].'
                &lt;/h2&gt;
            &lt;/div&gt;
        &lt;/div&gt;';

    }

}

echo '&lt;/div&gt;';

?&gt;
Simple Approach

The simplest thing to remove copy-pasted code is to extract the exact duplication to its own function. Any variables that are needed can be passed on:

function printFeedback($st, $i) {
    if(!empty($st[$i]['feedback'])) {

        echo '
        &lt;div class="col-lg-6 center"&gt;
            &lt;div class="col-sm-12 center" style="width: initial; background-color: ';

            if($st[$i]['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };

            echo '&gt;
                &lt;h2 style="font-size: 30px; color: #fff;"&gt;';
                    echo $st[$i]['feedback'].'
                &lt;/h2&gt;
            &lt;/div&gt;
        &lt;/div&gt;';

    }
}


Then use it:

$st = fetch([...]);

echo '&lt;div class="row center"&gt;';

//Output the first 2 Feedback on line 1;
for($i = 0; $i &lt; 2; $i++) {
    printFeedback($st, $i);
}

echo '&lt;/div&gt;&lt;br&gt;&lt;div class="row center"&gt;';

//Output the last 2 Feedback on line 2;
for($i = 2; $i &lt; 4; $i++) {
    printFeedback($st, $i);
}

echo '&lt;/div&gt;';


Now the duplication is gone. 

Further Improvements

Now, the simple approach did remove the direct duplication, but we still have two similar for loops, and we have a function that accepts arguments that do not have real meaning in that context. 

Let's improve it further by adjusting our print feedback function:

// echoes the given feedback array inside HTML.
function printFeedback($feedback) {
    if($feedback['feedback'])) {

        echo '
        &lt;div class="col-lg-6 center"&gt;
            &lt;div class="col-sm-12 center" style="width: initial; background-color: ';

            if($feedback['feedback_type'] == '+') { echo '#1AB394;"'; } else { echo 'red;"'; };

            echo '&gt;
                &lt;h2 style="font-size: 30px; color: #fff;"&gt;';
                    echo $feedback['feedback'].'
                &lt;/h2&gt;
            &lt;/div&gt;
        &lt;/div&gt;';

    }
}


Personally, I would create a real feedback object, but for this example I have kept it as an array, which may be fine for your purposes.

Now we can use this function in a more generic function:

// echoes the feedbacks in the feedback starting at position "from" and going to position "to" (exclusive).
function printFeedbackFromTo(Array $feedbacks, $from, $to) {
    for($i = $from; $i &lt; $to; $i++) {
        printFeedback($feedbacks[$i]);
    }
}


And then use that function:

$st = fetch([...]);

echo '&lt;div class="row center"&gt;';

printFeedbackFromTo(0,2);

echo '&lt;/div&gt;&lt;br&gt;&lt;div class="row center"&gt;';

printFeedbackFromTo(2,4);

echo '&lt;/div&gt;';


Misc


Feedback seems to be user-supplied, so you have to HTML encode it, to prevent XSS.
Your comments are not needed. Just extract code into properly named functions, possibly with proper function comments (ideally using PHPDoc). In-code comments should not be used to structure your code - use functions for that - but to point out possible unclarities, problems, etc that could not be resolved otherwise.
The functions you might want to consider would be getFeedbacks($connection, $amountOfFeedbacks) and echoFeedbacks($feedbacks).
Don't put your style info in a style attribute, but in an external CSS file