How can CSS be used to create different layouts for online and print versions of a webpage in PHP?

To create different layouts for online and print versions of a webpage in PHP, you can use CSS media queries to target specific styles for each version. By defining different styles for screen and print media types, you can control how the webpage appears on a screen and when printed. This allows you to optimize the layout and design for each medium.

<!DOCTYPE html>
<html>
<head>
    <style>
        /* Styles for online version */
        @media screen {
            body {
                background-color: lightblue;
            }
        }

        /* Styles for print version */
        @media print {
            body {
                background-color: white;
            }
        }
    </style>
</head>
<body>
    <h1>This is a webpage</h1>
    <p>Content goes here</p>
</body>
</html>