Jump to content


PHP, IE & CSS aren't playing nice


3 replies to this topic

#1 Will Gaffga

    Newbie

  • Members
  • Pip
  • 0 posts

Posted 31 August 2010 - 06:30 AM

Hi - I'm new here and would label myself as a relative novice with PHP. I am developing a new portfolio site for myself and am using 2 pages with PHP scripts to read the content of folders and display that information. Simple. The pages are well developed XHTML and CSS and work (mostly) fine with the major browsers except IE. (not even worth commenting)

I was really hoping someone here could provide some insight as to what's going on. I took efforts to work around the common IE/CSS bugs but never saw this coming.
Main page: http://www.wilyguy.n...olio/index.html
1 of 2 PHP pages: http://www.wilyguy.n...o/pdfs/docs.php
2 of 2 PHP pages: http://www.wilyguy.n...images/grfx.php

Note how bad they are in IE. Thoughts?

Also, if you happened to look at these in Chrome or FF, you may have noticed that the PHP pages do not render exactly the same as the HTML pages, they are offset vertically by something like -5 pixels. Again, I've been banging my head trying to figure this one out to no avail.

I suspect both of these are simply due to my PHP-naivete.

Thanks in advance,

Will.

#2 Shoel

    Administrator

  • Administrators
  • 125 posts

Posted 31 August 2010 - 09:15 PM

Hi Will,

Welcome to PHPTalk.com! :)

The IE weirdness is most likely because you are missing the DOCTYPE for your secondary pages.

If you add this (taken from your index) above the <head> tag of those pages, they should render ok.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >

As for the small difference between the menu placement between the index and secondary pages in firefox - that might just have the same cause.

Give it a try and let me know if it doesn't sort it - and I'll take a closer look for you. :)

- S.
Hi there! If you found this post useful, or used this information to help others, we would greatly appreciate a link back to our forum from your website/blog. Thanks! =)

#3 Will Gaffga

    Newbie

  • Members
  • Pip
  • 0 posts

Posted 02 September 2010 - 08:54 PM

Thanks for the response. I've been playing with this for a while now ... I figured something was going on with the doctype declaration but wasn't sure what.
I placed the text as you stated ... below the PHP statement and above <html>. That works and doesn't work.
Hunh?

If you place the complete range of text there, it doesn't work.
<?php
$xml="<?xml version='1.0' encoding='UTF-8'?>";
$doctype="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 
	'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>";
$html="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' >";
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
See the Graphics page for the results ... long story short, "Parse error: syntax error, unexpected T_STRING in /home/content/w/g/a/wgaffga/html/portfolio/images/grfx.php on line 7"

If you place the text except for the XML version and encoding statement, it works.
<?php
$xml="<?xml version='1.0' encoding='UTF-8'?>";
$doctype="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 
	'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>";
$html="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' >";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
See the Documents page.

I am glad that it works but any idea why it chokes when the statement is in?

Thanks again,

Will.

View PostShoel, on 31 August 2010 - 09:15 PM, said:

...
Give it a try and let me know if it doesn't sort it - and I'll take a closer look for you. :)

- S.


#4 Shoel

    Administrator

  • Administrators
  • 125 posts

Posted 03 September 2010 - 04:19 PM

It's because with your current PHP configuration <? (...) ?> is allowed as shorthand for <?php (...) ?> - thus...

<?xml version="1.0" encoding="UTF-8"?>
Within a .php file is like having..

<?php xml version="1.0" encoding="UTF-8" ?>
You can turn this behavior off at runtime using the following at the top of your file:

<?php
ini_set('short_open_tag', 0);
?>

Oh and why are you assigning the DOCTYPE etc to variables when you repeat it as plain markup outside the PHP?

In other words.. why..

<?php
$xml="<?xml version='1.0' encoding='UTF-8'?>";
$doctype="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 
        'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>";
$html="<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' >";
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
And not just..

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
or.. (assuming you want them assigned to separate variables before printing)

<?php
$xml     = "<?xml version='1.0' encoding='UTF-8'?>";
$doctype = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'\n 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>";
$html    = "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' >";

print $xml . $doctype . $html . '<head>';
?>

PS: For better readability you should put a space at either side of the operator like this:

<?php
$xml = "foobar"; // Good coding standard/practice.
$xml="foobar";   // Not so good..
?>
(You can also space it out a little extra before the "=" like I have done in my print example above - in order to bring the start of the strings in line and make it even tidier/easier to read.)

Oh and a useful tip for markup is delimiting strings using something called heredoc:

<?php
$header = <<<XHTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
XHTML;

print $header;
?>
This works equally well regardless of single or double quotes within the string, and supports variable substitution (just like a double-quoted string). It also preserves formating such as indention (tabs or spaces). The delimiter (in this case "XHTML") can be any word of your choosing.
Hi there! If you found this post useful, or used this information to help others, we would greatly appreciate a link back to our forum from your website/blog. Thanks! =)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users