views:

305

answers:

6

When I use a conditional statement targeting IE6 and below with some PHP code Google Chrome disregards the statement and inserts the code.

Example:

<!--[if lte IE 6]>
<?php require_once("ie6.php"); ?>
<![endif]-->

It will insert the content of ie6.php in the body anyway.

The code in ie6.php is something like this:

<?php print '<p>This is IE6</p>'; ?>

Anybody got this problem before?

+1  A: 

The PHP code is inserted way before the if statement is ever considered so it's always going to be there.

  1. Server evaluates the page and renders any PHP code
  2. The rendered html, including the contents of ie6.php, is sent to the browser
  3. The browser sees the lte IE 6 conditional and takes action
Mike B
+3  A: 

PHP will print <!--[if lte IE 6]> then include the "ie6.php" file then print <![endif]--> because that's exactly what you're telling it to do.

You're confusing which lines of code are run by the server and which are parsed by the browser.

Chrome ignores lines like <!--[if lte IE 6]> because they are targeted for IE browsers only.

webbiedave
Yes I know that... the problem was that Chrome was showing what is printed inside the conditional statements. Anyway I got the problem see below the solution.
Pedro Reis
+1  A: 

Browsers are the only things that use the conditional statements.

Because PHP is ran on the server, and then the resulting page is sent to the browser, the IE conditionals do not work.

Chacha102
A: 

This php code going to be included always, on the server side (regardless of the browser).

As far as I know conditional comments work on client side and browser doesn't have to respect them (I don't rememeber the details but I read somewhere that browser can do whatever it wants with HTML comments, even cut them out completely so people who put javascript into can be surprised).

If you want to decide whether to include file or not, use some browser detection: $_SERVER['HTTP_USER_AGENT'] or get_browser() would be a good place to start.

eyescream
A: 

The problem was that inside the html code in the ie6.php was another comment and Chrome didn't like it.

Something like this:

<?php print '<p>This is IE6 <!--a comment--> </p>'; ?>

It seems Chrome was interpreting the comment closing tag as the end tag and subsequently "misreading" the conditional statement.

Pedro Reis
A: 

i had a similar problem with chrome

doIEstuff

The comment inside the conditional comment was fine for FF and IE but it made chrome execute the code inside the conditional comment

Dan