Results 1 to 4 of 4

Thread: PHP within conditional comment

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Question PHP within conditional comment

    Is it possible for me to set a variable within a conditional comment?

    e.g.
    PHP Code:
    <!--[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\""; } ?>>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Um, why is the PHP necessary? Just put the class in conditional comments in the first place:
    Code:
    <!--[if IE 6]>
      <div class="mycssclass">
    <![endif]-->
    <!-- <![if !IE 6]> -->
      <div>
    <!-- <![endif]> -->
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Just put the class in conditional comments in the first place:
    Code:
    <!--[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:

    Code:
    <!--[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/libr...12(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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •