What are the common pitfalls to avoid when generating dynamic links in PHP loops?

One common pitfall to avoid when generating dynamic links in PHP loops is not properly concatenating variables and strings within the loop. This can lead to errors or broken links. To solve this issue, make sure to concatenate the variables and strings correctly to generate the dynamic links.

// Incorrect way of generating dynamic links within a loop
for ($i = 1; $i <= 5; $i++) {
    echo "<a href='page.php?id=$i'>Link $i</a>";
}

// Correct way of generating dynamic links within a loop
for ($i = 1; $i <= 5; $i++) {
    echo "<a href='page.php?id=" . $i . "'>Link $i</a>";
}