How can the <body class> attribute be effectively used to swap layouts in PHP?

To swap layouts in PHP using the <body class> attribute, you can create different CSS styles for each layout and then dynamically change the <body class> attribute based on certain conditions or user preferences. This allows you to easily switch between different layouts without having to duplicate code or create separate HTML files.

&lt;?php
// Check a condition to determine which layout to use
$layout = &#039;layout1&#039;; // Default layout

if ($someCondition) {
    $layout = &#039;layout2&#039;;
}

?&gt;

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Swap Layouts&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot;&gt;
&lt;/head&gt;
&lt;body class=&quot;&lt;?php echo $layout; ?&gt;&quot;&gt;
    &lt;header&gt;
        &lt;!-- Header content --&gt;
    &lt;/header&gt;
    
    &lt;nav&gt;
        &lt;!-- Navigation content --&gt;
    &lt;/nav&gt;
    
    &lt;main&gt;
        &lt;!-- Main content --&gt;
    &lt;/main&gt;
    
    &lt;footer&gt;
        &lt;!-- Footer content --&gt;
    &lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;