What is the correct way to read a file in PHP and use its contents in HTML code?

To read a file in PHP and use its contents in HTML code, you can use the file_get_contents() function to read the file contents into a variable, and then echo that variable within your HTML code. This allows you to dynamically include the file contents within your HTML output.

<?php
$file_contents = file_get_contents('file.txt');
?>

<!DOCTYPE html>
<html>
<head>
    <title>File Content Example</title>
</head>
<body>
    <h1>File Contents:</h1>
    <p><?php echo $file_contents; ?></p>
</body>
</html>