Will it have php code before doctype forces the browser to switch to quirks mode?

As discussed in one of the posts here; there was someone who dynamically bound the javacript code above the doctype declaration and then made his browser go into quirks mode. I am in the same state now, only in my case I am dealing with a piece of PHP code below what to be pasted over doctype, otherwise I would get a PHP "space" error.

<?php session_start(); if(!session_is_registered("reg")){ header("location:page1.php"); }?> <!DOCTYPE html> 

So, will this put my browser in quirky mode? If it were, is there any workaround? appreciate any help / suggestion. thanks.

+4
source share
4 answers

No. PHP code is processed before the browser uses it. Just make sure you don't send any trailing characters after your PHP or anything echoed.

+4
source

You can have PHP before doctype, PHP will not exist when it really matters. If you get some white space, just reformat the doctype right after closing ?> .

 <?php $var = 'foo'; ?><!DOCTYPE html> 

As long as you don’t output anything, the browser doesn’t even know that PHP appeared before the start of DOCTYPE, and that the browser doesn’t know will not damage it;)

+2
source

Placing a doctype on one line can help:

 <?php session_start(); if(!session_is_registered("reg")){ header("location:page1.php"); die(); // presumably you want this }?><!DOCTYPE html> 
+1
source

No, because php code is never printed. To verify this, right-click on the page and check the actual HTML source for the page. Javascript is different in that it really prints.

+1
source

All Articles