What are potential pitfalls when generating links with dynamic values in PHP?

When generating links with dynamic values in PHP, a potential pitfall is not properly escaping or sanitizing the dynamic values, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate this risk, always use proper sanitization functions like `htmlspecialchars()` or prepared statements when inserting dynamic values into links.

// Example of generating a link with a dynamic value properly sanitized
$user_id = $_GET['user_id']; // Assume user_id is coming from user input
$user_id = htmlspecialchars($user_id); // Sanitize the dynamic value
$link = "profile.php?user_id=" . $user_id; // Generate the link with the sanitized value
echo "<a href='$link'>View Profile</a>"; // Output the link