PHP conditional in HTML header question

i have a situation where i want to add an additional stylesheet in the header of a HTML/PHP page. i think i can add it using either of the following 2 ways…

<link rel="stylesheet" href="styles.css" type="text/css" />
<?php if ($_SESSION['screen_size'] == 'big') {
	echo '<link rel="stylesheet" href="more-styles.css" type="text/css" />';
} ?>

or…

<link rel="stylesheet" href="styles.css" type="text/css" />
<?php if ($_SESSION['screen_size'] == 'big') { ?>
<link rel="stylesheet" href="more-styles.css" type="text/css" />
<?php } ?>

can anyone tell me if both these ways are acceptable & if one of them is preferred?

Both should work fine.

I would go with the second approach as it handles quotes and double quotes and allows multiple lines.

But it’s really personal style.

cheers dude

@lancsDavid [COLOR=#0071D8]

[/COLOR]Not a PHP based solution but what you are doing with PHP can be achieved with C.S.S. Media Queries like:


/* LAYOUT -------------- */
#wrapper {    margin: 0 6%;}
#header {    margin: 0 0 20px 0;
    overflow: hidden;    padding: 0 0;
    position: relative;
}
.ie6 #header {
    width: 100%;
}
#nav-main {
    float: right;
    margin: 40px 0 0 0;
}
/* MEDIA QUERIES ----------------------------------- */
@media screen and (min-width: 1200px) { /*--- BIG SCREEN ---*/
   #nav-main {
       position: fixed;
      top: 260px;
       width: 13%;
      margin: 0;
        -moz-box-shadow: 0 0 8px hsla(0,0%,0%,.1);
        -webkit-box-shadow: 0 0 8px hsla(0,0%,0%,.1);
       box-shadow: 0 0 8px hsla(0,0%,0%,.1);
        -moz-border-radius: 3px;
        -webkit-border-radius: 3px; 
       border-radius: 3px;
        background: hsla(0,0%,100%,.3); 
       text-align: right; 
   }
   ... More CSS styles here ...
}
@media screen and (max-width: 760px) { /* -- Medium Size Say when a browser is not full screen on a desktop -- */
  em.header_img {
        float:left;
        display: block;
        background:  url(images/header_background_760px.jpg) no-repeat left top;
        width: 900px;
        height: 201px;
      }
  ... More CSS styles here ...
}
@media screen and (max-width: 550px) { /* -- Screen get smaller so lets get rid of some of the big asses and resize elements -- */
    em.header_img {
        display: none; 
   }
  ... More CSS styles here ...
}
@media screen and (max-width: 400px) { /* -- Smart Phone Sizing and Styles -- */
   #header {
      margin: 0; 
     overflow: hidden; 
      padding: 0 0;
      position: relative;
   }
  ... More CSS styles here ...
}
@media screen and (-webkit-min-device-pixel-ratio: 2) and (max-width: 480px) { /*-- Mobile Web-Kit Smart Phones -- */
  #header {
    margin: 0;
    overflow: hidden;
    padding: 0 0;
    position: relative;
  }
  ... More CSS styles here ...
}

[COLOR=#000000] For older browsers such as I.E. 6,7 and 8 there are pre-made javascript shims that can be used to give the same media query support to these browsers.

This is another way for you to consider doing it that off-loads some of your server processing to the browser.

Regards,
Steve[/COLOR]

I would also choose the second method, but with this caviot: I’d use the following code:

<link rel="stylesheet" href="styles.css" type="text/css" />
<?php if ($_SESSION['screen_size'] == 'big'): ?>
<link rel="stylesheet" href="more-styles.css" type="text/css" />
<?php endif ?>

That alternative conditional syntax is preferable, at least for me, when embedded in HTML.

@lancsDavid ,

I would go with the second method since it is easier to see what is happening, easier to debug and I cannot see the point of passing data to the browser if it is not required. Let your fast server do all the work and reduce your precious bandwidth.

Bandwidth will also be reduced for SmartPhones since the latter “more-styles.css” will only be downloaded for the “screen_size=big”.