1

My pset works fine, but I have a more 'theoretical' question about validating HTML code. When I initially pasted the code of my views into the validators, they came out as invalid (even though they worked fine). The issue was fixed as soon as I added a doctype declaration on top of every file.

However, I noticed that some of the staff's code does not have the doctype declaration on top, and yet it doesn't seem to be a problem.

Would someone mind explaining to me why that is the case? How can you have invalid code that still works? It's not like you're losing memory or sacrafising security here (I think) so I'm really curious :)

1

Instead of pasting the code of your views into the validator, try pasting the "right-click > view-source > select-all > copy" of your webpage into the validator. That will tell the validator the real story of what the webpage is composed of.

All the staff's code will execute a require("../views/header.php");, the first line of which is <!DOCTYPE html>.

Try pasting the source of your rendered code and you may find the validator complaining about two DOCTYPE declarations.

Browsers are very (very!) forgiving. There's gazillions of programmers (good and bad) writing gazillions of web pages (good and bad) in a multitude of languages for a multitude of browsers, with ever-evolving HTML/CSS to boot. If browsers complained about every little piece of invalid code, well, the internet would come to a screeching halt. Whether the pages "work" or not would be a very heated semantic discussion.

0

HTML is not like C -- you can have all sorts of mistakes in your markup, but most modern browsers will make an attempt to render it anyway. The general rule of thumb for web developers is to be liberal in what you accept as input, but strict about what you emit as output.

It's easy to see why browser developers would do this -- if they didn't, every website with a typo could disappear overnight! However, that's no excuse to publish sloppy markup. Invalid HTML can cause all sorts of subtle problems that can be difficult to debug. Worse yet, it could look fine in one browser, but break in another browser!

CS50 requires you to validate your HTML so that you can learn the right way to do it, even though many developers cut corners and get away with it.

If this answers your question, be sure to click the green check mark. Fight zombie questions!

EDIT: @DinoCoderSaurus: Actually, I think the validator complains about <!DOCTYPE HTML> and requires <!DOCTYPE HTML lang="en"> or something like that instead.

  • 1
    Since OP added a doctype declaration, I deduced that the validator was complaining about a missing declaration. lang is an attribute of the html element. From W3c.org Internationalization – DinoCoderSaurus Aug 26 '16 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .