What are some best practices for dynamically displaying subpages of a website in PHP without compromising security?

When dynamically displaying subpages of a website in PHP, it is important to sanitize user input to prevent SQL injection and other security vulnerabilities. One way to do this is by using prepared statements with parameterized queries to interact with the database. Additionally, it is recommended to validate and sanitize any user input before using it to fetch or display content on the website.

// Example of dynamically displaying subpages in PHP with proper security measures

// Sanitize and validate the input parameter
$subpage = filter_input(INPUT_GET, 'subpage', FILTER_SANITIZE_STRING);

// Prepare a parameterized query to fetch the subpage content from the database
$stmt = $pdo->prepare("SELECT * FROM subpages WHERE subpage_name = :subpage");
$stmt->bindParam(':subpage', $subpage, PDO::PARAM_STR);
$stmt->execute();

// Fetch and display the subpage content
$subpageContent = $stmt->fetch();

// Output the subpage content on the website
echo $subpageContent['content'];