What I Sent:
I have been a **. ** has been of great assistance in helping me make the transition from beginner to novice and now acts as a valuable resource on all things Linux. The tutorials section has some great articles, one of the most useful being the item you did on setting up the Xen virtual machine environment a few months back.
However, the PHP tutorial in this months magazine (March 2007), has left a somewhat sour taste in my mouth. I have been a professional PHP developer for nearly three years and often devote my time to assisting beginners; many of whom have been left frustrated and confused by poorly written tutorials. Overlooking the fact the author did opt to use PHP's PDO API - recently promoted in the magazine; in my opinion it does nothing to help the beginner other than show them how not to write a small web application.
Firstly, PHP's best features, that makes it preferable over other web development languages such as Perl, is the ability to be able to embed code inside HTML. Hence it is often described as an HTML embedded scripting language. So why is it the author of the feel it necessary to embed huge chunks of HTML code inside echo statements? This not only burdens the PHP interpreter with extra processing (as it needs to search for embedded variables within the string), it also destroys the logical flow of HTML, prevents syntax highlighters in text editors from highlighting the HTML and makes the code harder to read and ultimately maintain.
It's not as if PHP doesn't provide the tools to properly format HTML code. The developers have included alternate template like constructs (http://www.php.net/alternative+syntax/) designed to replace curly braces when generating large portions of HTML. This means the PHP processing instructions can become an integral part of the HTML itself:
<?php if ($age < 18):?>
<p>You are <?php echo($age) ?> and too young to access this resource.
<?php endif; ?>
I cannot stress the importance of correct use of print and echo within PHP. Even the smallest of applications reap the benefits of using the proper approach and it makes creating large scale, de-coupled applications that follow the MVC design pattern much simpler.
Secondly, the mark-up generated is not HTML, as it lacks <html>, <head> and <body> elements. In an industry driven by standards, this omission is simply not acceptable and any unsuspecting beginner will come down to Earth with a bump when they need to generate more complex layouts.
I am a firm believer of the saying "old habits die hard", and those habits acquired during the learning stage of any language are the most difficult to kill. I am also of the belief that those of us who supply tutorials and articles to the PHP community have a responsibility to help ensure they promote good coding techniques from the outset. As such, I have taken the time to rewrite the scripts and have attached these complete with comments to this email. I have also published the working application on (http://php5.codedv.com/lfx/)
I hope this revised code can be of use to other readers.
What they published:
The PHP tutorial in this month’s magazine has left a somewhat sour taste in my mouth. In my opinion it does nothing to help the beginner, other than show them how not to write a small web application.
Firstly, PHP’s best feature is the ability to embed code inside HTML. So why does the author feel it necessary to echo huge chunks of HTML code inside echo statements? This not only burdens the PHP interpreter with extra processing, it also destroys the logical flow of HTML, prevents syntax highlighters from highlighting the HTML and makes the code harder to read and ultimately maintain. It’s not as if PHP doesn’t provide the tools to properly format HMTL code. The developers have included alternate template-like constructs designed to replace curly braces when generating large portions of HTML.
Secondly, the mark-up generated is not HTML, as it lacks <html>, <head> and <body> elements. Any unsuspecting beginner will soon come down to earth with a bump when they need to generate more complex layouts.
The Authors Reply
Thanks for your email. Although there isn’t space here to reply to everything you’ve said. I’d like to make a few points:
This is a tutorial for MySql programmers who want to dip their toes into PHP. In four pages there simply isn’t enough room to include discussion of valid vs invalid HTML.
When you say “huge chunks of HTML” I’m not sure what you’re referring to there are only 20 line of PHP-wrapped HTML split across the entire tutorial, which is hardly huge, and even then many of them were repeated to make a point clear.
Saying it “burdens the PHP interpreter with extra processing” is rubbish. Write a script that does the echo loop 1000 times, then time it. We did it her as a test and found the PHP-wrapped loop took 0.049 seconds to execute, and the non wrapped loop took 0.057 seconds to execute; it was actually slower. But running it again yielded the opposite results. This happens because the difference between the two is so minuscule that it is dwarfed by virtually every other factor involved in serving the page.
PHP alternative syntax is rarely used, and would have complicated the tutorial. PHP’s standard syntax is similar to Javascript, C, C++, Java and many other C-like languages and as such is recognisable to many people. Please also keep in mind the official PEAR coding standards “strongly encourage” programmers to use curly braces even when they are optional.
MySql users who want to dabble with PHP really just want to have fun and see what it can do; rather than be weighed down with talk about alternative syntax and PDO.