2010年12月24日星期五

转载一个文章 “Python vs. PHP”


Posted on 30th March 2005 by Nio in 程序人生

PHP Everywhere 贴出一篇《Python never had a chance against PHP》的文章,这并不是在对 Python 进行挑衅,而是较为客观地对 PHP 和 Python 进行分析,认为在 Web 应用方面,Python 比不上 PHP:


Python is not a template language, in the sense that you cannot mix code and html easily. PHP is a wonderfully flexible in this respect.

Python is a so-so string processing language. One reason being it treats strings as immutable. PHP has much better string processing facilities: embedded "$var in strings", mutable strings, auto-conversion of other data types to strings, output buffering, etc.

PHP's documentation is cleaner and much easier to understand than Python's. Probably because PHP is a much simpler language.

PHP has tighter integration of a lot of web related stuff. For example, HTTP and SERVER variables.

对于各种语言,我的观点从来都是:存在即是合理的,每个语言都有其应用的空间。就对于 Python 而言,在 Web 方面不如 PHP,但其在通用编程方面功能却要比 PHP 强大得多。而现在流行 Web 编程,所以 PHP 这方面的优势也就决定了增长势头要比 Python 猛得多。



关于此文章的评论也很值得一看。I'll certainly concede that PHP has some important advantages over Python, especially in terms of deployment, isolation, and the ease of starting projects. But I can't agree with much of these particular reasons.


* Templating language: this is true, though you can template perfectly well in Python ― there exist PHP-like systems, as well as templating languages implemented on top of Python. Python itself isn't a templating language so there's no single convention. Of course, even PHP has things like Smarty.

* Python is way, way, WAY better at handling strings compared to PHP. This is the thing that has always driven me crazy in PHP. The $'s are nice ― except for the quoting rules so that $'s aren't interpreted in single quoted strings. That drives me nuts. Python does very good auto-conversion in interpolated strings (%s), and explicit conversion is easy enough. Python also has lots of techniques for buffering ― the specific pattern that PHP uses isn't common, but it's easy to do anyway (StringIO). Python has a concise and fast set of string methods, it has decent regular expression support (more convenient than PHP's at least), and it has really good unicode support. Oh, and NO MAGIC QUOTING. Quoting bugs don't come up that often in Python.

* I don't know what to say about the documentation. I like php.net's comments. I find the original documentation quite poor, and obviously the structure of the language is non-existant, lacking namespaces, modules, classes for most (all?) core types, and other basic organization. Also, most of the Python standard library is implemented in Python, and the source is readable. But it's not an uncommon complaint, so apparently it's something that bothers people.

* Don't good PHP programmers avoid the HTTP and SERVER variables? (register globals or whatever it is) If you just mean a global variable with those values from the request ($_POST?), that's easy in Python, and some frameworks use that. Python works with markup, like HTML and XML, better than PHP ― that's the kind of thing I'd think of if I was thinking about web integration.


� Ian BickingPlease don't forget:


* Smarty is just a rework of PHP itself - its a templating language written in a language already capable of templating. A pointless project from the start, in my opinion, which increases the workload, development time, execution time, and adds an aditional layer where issues can occur.

* Single quotes are there for a reason - so that we don't have to escape special characters all the time. I use single quotes and concattenation for everything, which can be a major speed increase when working with strings in PHP. We can, and its recommended to, turn Magic Quoting off.

* PHP has released a version of its original documentation which incorporates the comments from the website. I find this very useful. However, it only works on windows.

* As for the structure of PHP… IMHO it makes it easy for people to get started, and then (if they wish, as I did) to move to more structured development techniques, then on to other lanugages. I've recently started to look at Python as my next development language, after PHP, JavaScript (Client-side), XSL, and XQuery. Without PHP's quick learning curve, I don't think I'd have ever gotted started.

* No, we don't avoid the global variables, we just avoid the depricated ones. $HTTP_SERVER_VARS and the like. Register globals is the method that allows any parameter passed to a script in GET or POST format to be instantly accessible within the script. This is a serious security threat, and therefore we access them now through the $_POST and $_GET arrays. Variables such as $_SERVER allow us access to information which is passed around via http headers from client to server. Very handy.

* With the advent of PHP5, working with HTML and XML has become very easy. We can now quickly populate objects with a DOM, and modify elements/nodes and attributes with ease. We can also perform XPath queries easily on them. XSLT has been around for a while too, so a lot on the time things can be collected and translated without PHP doing much work.


PHP has its place, and in the future I hope that it will grow to incorporate some of the benefits of other languages, such as persistance. But for the moment while developing for the web, I'll first look at whether PHP can provide a solution before considering other platforms.

没有评论:

发表评论