I once had to work with some very poorly written code, and I found this gem of a coding horror.
Within the PHP code that printed the HTML markup, there was a section that printed JavaScript. What did the JavaScript do? It selected an element (which had just been printed in the code above) and set the height attribute to a value based on the number of items inside the box. So you have PHP writing JavaScript which sets a style. That’s three technologies to do one simple thing. And the best part of it all: it was totallly unnecessary! I couldn’t understand why the height needed to be set, so I went ahead and set it to auto (the default value), and it worked just fine. (Likely, the items within the box were originally floated, which means they’d be removed from the normal flow, requiring a set height). Needless to say, I removed the entire mess. (to give the developer credit, he was clearly a smart guy and wrote some good code; I’m not sure why he resorted to this elaborate setup)
But let’s say you did float the elements within the box. Then what? Add an empty element after all the floated elements, like so: <br style="clear:both" />. (or better yet, put it in your CSS). The clear attribute ensures the element appears below the floated elements, and so the containing box gets an appropriate height.
Talking about this stuff reminds me how nice HTML layouts are. You almost never have to set the height or width of elements (unless you’re doing something tricky). Contrast this with Flash and ActionScript (to name just one example), in which you must explicitly set a height and width on everything, which is particularly annoying when you have dynamically sized content.
I’ve been doing a bit of Java work lately; I built a quick prototype in PHP and needed to convert it into Java. And I was shocked at a few things that are trivial in PHP but more difficult in Java. An example: I have text which may or may not (but probably does) contain HTML, and I want to simply remove all the tags to create “unformatted text”. In PHP this is as simple as
$unformatted_text = strip_tags($text_with_html);
Easy! It’s just one line! Now how’s it work in Java? I won’t go into detail in this post, but you can read a solution on stackoverflow. It involves downloading and importing a third party package (is javax third party?) that allows you to parse the HTML, and you need to be sure to set a somewhat obscure argument that will ignore all tags. This isn’t difficult, it’s just more work, and it gets in the way of my getting things done.
Another example I came across in the same project is encoding and decoding JSON. Again, PHP has built in functions json_encode and json_decode. Java, again, you need to download and import third party packages.
I’m not complaining. These aren’t deal breakers. I’m just very surprised. When I first began working with PHP, I didn’t really like the language (Have you ever created an anonymous function in PHP? And those dollar signs in front of variables, the hell?) But now I realize how much I’ve taken PHP for granted. Web development with PHP is easy.