PDA

View Full Version : PHP within conditional comment



jc_gmk
10-01-2007, 02:19 PM
Is it possible for me to set a variable within a conditional comment?

e.g.


<!--[if IE 6]>
<?php $myvariable = "yes" ?>
<![endif]-->


The problem I have is that my webpage works fine in every other browser exept for IE6!
all I am trying to do is reposition a div if they use IE6

The idea I had was to set the class of the div depending on wether they are using IE6
e.g.

<div <? if ($myvariable == "yes") { print "class=\"mycssclass\""; } ?>>

Twey
10-01-2007, 04:38 PM
Um, why is the PHP necessary? Just put the class in conditional comments in the first place:
<!--[if IE 6]>
<div class="mycssclass">
<![endif]-->
<!-- <![if !IE 6]> -->
<div>
<!-- <![endif]> -->

djr33
10-01-2007, 06:15 PM
The answer is no.

You could try to detect the user agent with PHP (in the $_SERVER array), but that's not as reliable as a conditional comment.

I'd recommend having the output, with conditional comments if necessary, work in all browsers, rather than having PHP try to fix it for IE.

Twey's suggestion should work.

msmith0957
08-15-2010, 09:32 PM
Just put the class in conditional comments in the first place:
<!--[if IE 6]>
<div class="mycssclass">
<![endif]-->
<!-- <![if !IE 6]> -->
<div>
<!-- <![endif]> -->

Sorry to drudge up an old thread.. but I found this page along my search and was using it for reference but still couldn't figure out why this wasn't working. I was instead using conditionals to control which image would show, a gif or png depending on ie 6 or not and found that in IE 6, both images would show, but looked fine in modern browsers. The problem was obviously the second part of the conditional here. The fix is simple, just remove the <!-- and --> from the !IE sides of the conditional, like so:



<!--[if IE 6]>
<div class="mycssclass">
<![endif]-->
<![if !IE 6]>
<div>
<![endif]>


Anyone, please correct me if I'm mistaken, but its the only way I got it to work. With that code, in Dreamweaver you will see the !IE be regular html and not commented out like the conditional before it.

For more on IE's Conditional Comments: http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx

I found this line entertaining:


Although it would be a nice idea to be able to use the <!if !IE]> statement, conditional statements are a creation of Microsoft that have not been standardized by the W3C and therefore have to date not been adopted by other browser manufacturers. They therefore will never be executed or made visible in Firefox since Mozilla has never implemented them in their browsers and probably never will unless the W3C decides to standardizes them or until they become a W3C recommendation. Naturally if all browser manufacturers would make their browsers fully W3C standards compliant there wouldn't be a real need for Conditional statements.