I really hate it when people say flat out that PHP is a terrible language because you can write code which mixes presentation with logic, or that it lets you allow SQL injection. That's nothing at all to do with the language, that's the developer.
PHP has proved itself to be highly scalable: Wikipedia is one of the largest and most popular sites on the Internet and it runs PHP. Enough said?
There are a lot of tools/libraries out there that give you a framework to work in, making it less likely that someone will write poor, less-maintainable code: see CakePHP, Symfony, PDO, Smarty, etc etc etc.
It has received a bad rap because it's a language which has very low barriers to entry: it's free, you can get very cheap PHP hosting, the documentation is the best there is, there's plenty of tutorials online, plus it makes a lot of things very easy (eg: open a URL and get the contents of the file: file('http://www.google.com');). This means that a lot of newbies picked it up and made a lot of very dodgy sites with it, but that's going to happen with whatever language you choose as your first.
Work with a solid ORM framework (there's about 30 questions on SO about which is best), and it will treat you good.
A lot of people who say not use it are really saying don't use PHP 4. It comes down to this
you can write good code in any language
and
you can write bad code in any language
PHP can very often lend itself to becoming tangled spaghetti code libraries and make your 'application' really just a series of scripts (see Moodle for a good example of this...)
I think a lot of the 'Don't use PHP for large stuff' is coming from PHP being hacked up from it's original purpose: a templating language. Which I can understand, but there are lots of projects that prove you can do it (Drupal, mediawiki, Facebook).
哦,这是另一个问题:PHP 并不真正支持后台处理或线程。 您需要其他东西(或独立脚本)。 如果您正在使用其他东西,为什么不将其也用于 Web 内容(例如 Java、Ruby、.Net 等)?
Theres no reason you can't use PHP for large projects. After all, Facebook is built on PHP. There will be issues however but there are issues with any large project.
What makes PHP so pervasive is the low barrier to entry and cheap hosting. It runs as an Apache extension and you can pretty much just start coding. If you go to more enterprise platforms such as .Net or Java, they have a much higher barrier to entry but they also come with a lot of infrastructure to help you make applications that scale.
For example, the database abstraction in PHP is (imho) woeful. It's vendor specific. With MySQL, people tend to do things like:
function get_users($surname) {
mysql_query("select * from users where surname = '$surname'");
...
}
which is bad for several reasons:
It makes poor use of the query cache;
It doesn't handle escaping of characters (which, of course, can be done with mysql_escape_string() but you'll be surprised how often people don't do this); and
It's fairly easy to code in such a way as to allow SQL injection attacks.
Personally I prefer mysqli for all the above reasons but it has it's own problems: namely that using LONGTEXT fields crashes mysql and has done since at least 2005 with still no fix (yes I and several others have raised a bug).
Compare this to Java (with which I'm more familiar) and JPA or Ibatis are vastly better ORM solutions with higher startup costs but they will help you on an enterprise scale.
So you're not prohibited from doing large projects on PHP. It's simply harder in that you have to do increasingly more work yourself to replicate what other platforms provide you.
That being said, PHP + memcached/APC + beanstalkd goes a long way.
Oh that's the other problem: PHP doesn't really support background processing or threading. You need something else for that (or standalone scripts). If you're using something else, why not use that for the Web stuff too (eg Java, Ruby, .Net, etc)?
I made a tongue-in-cheek comment in another question thread calling PHP a terrible language and it got down-voted like crazy. Apparently there are lots of people here who love PHP.
So I'm genuinely curious. What am I missing? What makes PHP a good language?
Here are my reasons for disliking it:
PHP has inconsistent naming of built-in and library functions. Predictable naming patterns are important in any design.
PHP has inconsistent parameter ordering of built-in functions, eg array_map vs. array_filter which is annoying in the simple cases and raises all sorts of unexpected behaviour or worse.
The PHP developers constantly deprecate built-in functions and lower-level functionality. A good example is when they deprecated pass-by-reference for functions. This created a nightmare for anyone doing, say, function callbacks.
A lack of consideration in redesign. The above deprecation eliminated the ability to, in many cases, provide default keyword values for functions. They fixed this in PHP 5, but they deprecated the pass-by-reference in PHP 4!
Poor execution of name spaces (formerly no name spaces at all). Now that name spaces exist, what do we use as the dereference character? Backslash! The character used universally for escaping, even in PHP!
Overly-broad implicit type conversion leads to bugs. I have no problem with implicit conversions of, say, float to integer or back again. But PHP (last I checked) will happily attempt to magically convert an array to an integer.
Poor recursion performance. Recursion is a fundamentally important tool for writing in any language; it can make complex algorithms far simpler. Poor support is inexcusable.
Functions are case insensitive. I have no idea what they were thinking on this one. A programming language is a way to specify behavior to both a computer and a reader of the code without ambiguity. Case insensitivity introduces much ambiguity.
PHP encourages (practically requires) a coupling of processing with presentation. Yes, you can write PHP that doesn't do so, but it's actually easier to write code in the incorrect (from a sound design perspective) manner.
PHP performance is abysmal without caching. Does anyone sell a commercial caching product for PHP? Oh, look, the designers of PHP do.
Worst of all, PHP convinces people that designing web applications is easy. And it does indeed make much of the effort involved much easier. But the fact is, designing a web application that is both secure and efficient is a very difficult task.
By convincing so many to take up programming, PHP has taught an entire subgroup of programmers bad habits and bad design. It's given them access to capabilities that they lack the understanding to use safely. This has led to PHP's reputation as being insecure.
(However, I will readily admit that PHP is no more or less secure than any other web programming language.)
What is it that I'm missing about PHP? I'm seeing an organically-grown, poorly-managed mess of a language that's spawning poor programmers.
So convince me otherwise!
Top Rated Answer
I'll take a stab at responding to each of your bullet points
PHP has inconsistent naming of built-in and library functions. Predictable naming patterns are important in any design.
I both love and hate this topic. Because at its core, this issue is correct. Why are some bi-word function split with an underscore, and some aren't? Why do needle and haystack parameters swap positions in the argument signature sometimes? It's ridiculous. But at the end of the day... does this really matter? My IDE with intellisense and php.net just a browser click away, this is just plain not that big of a deal. Is it a negative against PHP as a language? Yes. Does it hinder my ability to be an effective programmer? No.
The PHP developers constantly deprecate built-in functions and lower-level functionality. A good example is when they deprecated pass-by-reference for functions. This created a nightmare for anyone doing, say, function callbacks.
Personally, I think this is not a good point. Deprecation is necessary to the evolution of a language, especially one that has as much kruft as PHP does. PHP gets a lot of flak for "making it easy to be a bad programmer*" but at the same time, the PHP group also gets in trouble when they try to remove stupid constructs from the language, such as call-time pass-by-reference. Eliminating call-time pass-by-reference was one of the best moves they ever made. There was no easier way for a novice developer to shoot themselves in the foot than with this "feature".
A lack of consideration in redesign. The above deprecation eliminated the ability to, in many cases, provide default keyword values for functions. They fixed this in PHP 5, but they deprecated the pass-by-reference in PHP 4!
I don't think there's a general lack of consideration at all, I think you just got stung by this particular change and have been left with a sour taste in your mouth. Language changes are often known months if not years ahead of time. A migration guide was provided for the move from 4 to 5, and the version differences are documented in the manual. Call-time pass-by-reference was a horrible "feature" and doesn't give the developer any expressive power they can't get by other means. I'm glad it is gone (along with other crap like magic quotes)
Poor execution of name spaces (formerly no name spaces at all). Now that name spaces exist, what do we use as the dereference character? Backslash! The character used universally for escaping, even in PHP!
I have mixed feelings about this. Part of me thinks "who cares, character escaping has no meaning outside of a string anyway", and part of me thinks "surely they could use something better". But could they? I don't know, I'm not a developer for the Zend parser. Is it a huge oversight that until 5.3 PHP never had namespaces at all? Yes, absolutely.
Overly-broad implicit type conversion leads to bugs. I have no problem with implicit conversions of, say, float to integer or back again. But PHP (last I checked) will happily attempt to magically convert an array to an integer.
I think it's ok to disagree with how PHP does this, but disagree that it makes the language "bad". But ask me how much I want to sit in this topic and argue about weak vs strong typing. (P.S. I don't, at all) For the record: PHP will issue an E_WARNING level error when the type of an argument matters and cannot by solved by coercion.
Poor recursion performance. Recursion is a fundamentally important tool for writing in any language; it can make complex algorithms far simpler. Poor support is inexcusable.
PHP is a DSL for the web. I've been doing it full-time for 8 years and have maybe used recursion 4 or 5 times, usually for some type of annoying directory or XML traversal. It's just not a pattern that is needed for web development that often. I'm not excusing the slow performance, but this is an academic issue far more than it is a production issue. If you need really powerful recursive performance, PHP is already the wrong language for you.
Functions are case insensitive. I have no idea what they were thinking on this one. A programming language is a way to specify behavior to both a computer and a reader of the code without ambiguity. Case insensitivity introduces much ambiguity.
I totally 100% agree with this.
PHP encourages (practically requires) a coupling of processing with presentation. Yes, you can write PHP that doesn't do so, but it's actually easier to write code in the incorrect (from a sound design perspective) manner.
*Hmmm, this topic sounds desperately familiar...
But seriously, I find it remarkable that people will complain about a language that will absolutely 100% let you implement any output system you want (the sheer volume and style of PHP templating systems alone speaks to this) - OR - skip all that overhead and just output directly. This does not make PHP bad at all. It's part of what makes PHP good.
PHP performance is abysmal without caching. Does anyone sell a commercial caching product for PHP? Oh, look, the designers of PHP do.
Do you mean bytecode caching (like an accelerator), or output caching?
If the former, then I don't really know how much I care about this topic. Accelerators are free and easy to run. We could argue about why it isn't part of the language but in the end, I don't think it matters much.
If you are talking about output caching then I don't know what to say to you. ANY web project with significant traffic needs caching (seed podcast #27, for example). This is not a PHP-specific issue at all.
In summary, I think you consider PHP a "bad" language in a very academic fashion. And in your previous post you were probably voted down by people like me who use PHP to "get things done".
Second Top Rated Answer
All your criticisms (and some more) are valid. You are allowed and even expected to hate PHP.
But, then again, it has some benefits:
Ubiquitous
Fast (especially using opcode caches)
Huge community (and great documentation)
Works
Finally, you can overcome many if not all the downsides by writing good code you'd write in any other language. You can write solid, secure and good smelling code in PHP, which many times will run faster and be easier to host and to scale than many alternatives.
Third Top Rated Answer
What is it that I'm missing about PHP? I'm seeing an organically-grown, poorly-managed mess of a language that's spawning poor programmers.
Simple. The fact that poor programmers get very defensive about their language. ;) PHP is easy to learn, much easier than the alternatives, and once you've learned it, it's not exactly obvious 1) what's wrong with PHP, 2) how the alternatives are better, and 3) how to switch to, and learn, one of the alternatives.
And perhaps the fact that, well, what alternatives do people have? ASP? That has plenty of problems on its own, from being unable to run on the majority of webservers (Apache), to some ridiculous and overengineered design choices on its own (webforms? Viewstate? AJAX where your asynchronous" requests are intercepted and run sequentially?) Ruby on Rails? Well, perhaps, except how many webservers support it again? It's not exactly easily approachable at the moment. And it's slow. So perhaps PHP's "strength" is really that no good alternative exists. At least this is why I stay away from all web programming when at all possible. PHP sucks, and I'm not too keen on any of the alternatives either.
PHP has so many fundamental problems that it's not even funny. From the lack of unicode support, to the many implicit type conversions which often lead to unexpected security holes, to the complete mixing of presentation and... everything else, or to the default database module which doesn't (last I checked) use parametrized queries. We're talking about a language made for two things, database access and generating HTML, and which is terrible at both.
It's just a nasty mess, a language designed by people who aren't qualified, or able, to design a language. ;)
For me the worst PHP sin is coupling of presentation with business logic. It's not that you can't write it in a better way, but it doesn't encourage you to, and if anything it encourages you not to.
A large number of security vulnerabilities are associated with PHP sites, too. I can't prove it is disproportionate (after all a lot of sites are written in PHP), but I suspect it is. If I'm right, then since security vulnerabilities are a class of bug, I suspect PHP sites tend to be more buggy on the whole also.
(I don't think that pointing at a few large sites and saying they managed to do it in PHP is any argument against this, by the way. It's a bit like saying that cigarettes don't cause cancer because your next door neighbour smoked and lived to be 100.)
Recapping - Facebook, Wikipedia, Yahoo.com, Digg, Flickr and many other giant sites are running on PHP. If you ever come close to making something of that caliber, you can still rest assured you can get there with PHP.
How maintainable, extendable, reliable, secure and performant your applications will be is entirely up to you and is language-agnostic. In favor of PHP though, it has very convenient tools for building web applications.
For me, and talking about large or even huge projects, it (primarily) boils down to one word: Dependencies.
The problem with a scripting language is like in every thing in the world: The greatest advantage is the greatest disadvantage at the same time.
The greatest advantage is to code free and fast. Just write a script and it will server it's purpose. No verbosity needed, simply code.
The greatest disadvantage is, in a way, to check if this script is not disturbing other scripts. Or better: Change an old script others are relying on. Are you sure that all dependencies do work out as you desired?
This is not true for "normal" web page generation, whatever normal means here. But we have a product relying on some 500k lines of source code, with customizations for clients consisting of an additional 100k lines of code, too. And I am deadly glad that a compiler checks all dependencies and warns/errors me in case I did something wrong (like, speaking lowest level here, misstyping a variable or method call).
I think this and the fact that other languages provide more simple-to-use "enterprisy" features by their nature (i.e. application servers for "bank usages") boils it down why many do not see PHP in large (or better: huge) projects.
There's something about the construction of the PHP language that is not good enough for me. For example, the name of the functions. It is a non best practice use a several ways to name a function in one language. The mixture between underscores (function_name), words stick together (functionname), etc.I mean, this is really a mess. There are too many functions that are very similar or do the same things, but their names are so confusing. This is not characteristic of a good programming language.
In large deployments, the language must be enough easy and specific to write. Something that PHP omits like the declaration of variable types, become very difficult to understand and deal with later.
Other point is the constant addition of features and canceling of some other. It supposes that the addition of OOP in PHP 5 gonna make the things easier for programmers, but what about the considerations of back compatibility?
The principal reason that this programming language is like it is, is due to its origins: Personal Home Page. It was not designed for large deployments.
I know that there are great efforts to make this language an enterprise-weight language, and personally, I'm waiting for a good enough open source server-side programming language; but until this day comes, it's gonna run a lot of water under the bridge.
I was a newbie. I've only been coding for 5 years but I directly support and manage 85 small to large websites and I'll tell you what, the potential to get sued by having a website down for a day will contribute a lot to your desire to learn how to and make better code.
It's nice to hear established developers share their thoughts on this matter. I don't think PHP is the best, but it seems my investment in "best practices" is well served.
发布评论
评论(11)
我真的很讨厌人们直截了当地说 PHP 是一种糟糕的语言,因为你可以编写将表示与逻辑混合在一起的代码,或者它允许你允许 SQL 注入。 这与语言无关,而是开发人员的问题。
PHP 已经证明了自己具有高度可扩展性:维基百科是 Internet 上最大、最受欢迎的网站之一,它运行 PHP。 说够了?
有很多工具/库可以为您提供一个工作框架,从而减少有人编写糟糕的、难以维护的代码:请参阅 CakePHP、Symfony、PDO、Smarty 等。
它已收到一个坏名声,因为它是一种进入门槛非常低的语言:它是免费的,你可以获得非常便宜的 PHP 托管,文档是最好的,网上有很多教程,另外它使很多事情变得非常简单(例如:打开 URL 并获取文件的内容:
file('http://www.google.com');
)。 这意味着很多新手开始使用它并用它制作了很多非常危险的网站,但是无论您选择哪种语言作为第一种语言,这种情况都会发生。使用可靠的 ORM 框架(关于哪个最好的问题,大约有 30 个问题),它会给你带来好处。
I really hate it when people say flat out that PHP is a terrible language because you can write code which mixes presentation with logic, or that it lets you allow SQL injection. That's nothing at all to do with the language, that's the developer.
PHP has proved itself to be highly scalable: Wikipedia is one of the largest and most popular sites on the Internet and it runs PHP. Enough said?
There are a lot of tools/libraries out there that give you a framework to work in, making it less likely that someone will write poor, less-maintainable code: see CakePHP, Symfony, PDO, Smarty, etc etc etc.
It has received a bad rap because it's a language which has very low barriers to entry: it's free, you can get very cheap PHP hosting, the documentation is the best there is, there's plenty of tutorials online, plus it makes a lot of things very easy (eg: open a URL and get the contents of the file:
file('http://www.google.com');
). This means that a lot of newbies picked it up and made a lot of very dodgy sites with it, but that's going to happen with whatever language you choose as your first.Work with a solid ORM framework (there's about 30 questions on SO about which is best), and it will treat you good.
很多说不使用它的人实际上是在说不要使用 PHP 4。归根结底,
你可以用任何语言编写好的代码
,
你可以用任何语言编写糟糕的代码。 PHP
经常会变成混乱的意大利面条代码库,并使你的“应用程序”实际上只是一系列脚本(请参阅 Moodle 以获得一个很好的例子......)
我认为很多“Don “不要使用 PHP 来做大事情”是因为 PHP 的最初用途被破坏了:一种模板语言。 我可以理解,但是有很多项目证明你可以做到(Drupal、mediawiki、Facebook)。
A lot of people who say not use it are really saying don't use PHP 4. It comes down to this
you can write good code in any language
and
you can write bad code in any language
PHP can very often lend itself to becoming tangled spaghetti code libraries and make your 'application' really just a series of scripts (see Moodle for a good example of this...)
I think a lot of the 'Don't use PHP for large stuff' is coming from PHP being hacked up from it's original purpose: a templating language. Which I can understand, but there are lots of projects that prove you can do it (Drupal, mediawiki, Facebook).
没有理由不能将 PHP 用于大型项目。 毕竟,Facebook 是基于 PHP 构建的。 然而,总会有问题,但任何大型项目都会有问题。
PHP 如此普及的原因在于其低门槛和廉价托管。 它作为 Apache 扩展运行,您几乎可以开始编码。 如果您使用 .Net 或 Java 等更多企业平台,它们的进入门槛要高得多,但它们也配备了大量基础设施来帮助您开发可扩展的应用程序。
例如,PHP 中的数据库抽象(恕我直言)很糟糕。 这是供应商特定的。 对于 MySQL,人们往往会做这样的事情:
由于以下几个原因,这很糟糕:
就我个人而言,由于上述所有原因,我更喜欢 mysqli,但它有自己的问题:即使用 LONGTEXT 字段会导致 mysql 崩溃,并且至少从 2005 年开始就已经这样做了,但仍然没有修复(是的,我和其他几个人提出了一个错误)。
与 Java(我更熟悉)相比,JPA 或 Ibatis 是更好的 ORM 解决方案,启动成本更高,但它们将在企业规模上为您提供帮助。
因此,您不会被禁止使用 PHP 进行大型项目。 这只是更困难,因为你必须自己做越来越多的工作来复制其他平台为你提供的东西。
话虽这么说,PHP + memcached/APC + beanstalkd 有很长的路要走。
哦,这是另一个问题:PHP 并不真正支持后台处理或线程。 您需要其他东西(或独立脚本)。 如果您正在使用其他东西,为什么不将其也用于 Web 内容(例如 Java、Ruby、.Net 等)?
Theres no reason you can't use PHP for large projects. After all, Facebook is built on PHP. There will be issues however but there are issues with any large project.
What makes PHP so pervasive is the low barrier to entry and cheap hosting. It runs as an Apache extension and you can pretty much just start coding. If you go to more enterprise platforms such as .Net or Java, they have a much higher barrier to entry but they also come with a lot of infrastructure to help you make applications that scale.
For example, the database abstraction in PHP is (imho) woeful. It's vendor specific. With MySQL, people tend to do things like:
which is bad for several reasons:
mysql_escape_string()
but you'll be surprised how often people don't do this); andPersonally I prefer mysqli for all the above reasons but it has it's own problems: namely that using LONGTEXT fields crashes mysql and has done since at least 2005 with still no fix (yes I and several others have raised a bug).
Compare this to Java (with which I'm more familiar) and JPA or Ibatis are vastly better ORM solutions with higher startup costs but they will help you on an enterprise scale.
So you're not prohibited from doing large projects on PHP. It's simply harder in that you have to do increasingly more work yourself to replicate what other platforms provide you.
That being said, PHP + memcached/APC + beanstalkd goes a long way.
Oh that's the other problem: PHP doesn't really support background processing or threading. You need something else for that (or standalone scripts). If you're using something else, why not use that for the Web stuff too (eg Java, Ruby, .Net, etc)?
由于我链接的问题已被删除,我将其中一些放在这里:
问题
我在另一个问题线程中做了一个半开玩笑的评论,称 PHP 是一种糟糕的语言,但它疯狂地被否决了。 显然这里有很多人喜欢 PHP。
所以我真的很好奇。 我缺少什么? 是什么让 PHP 成为一门好语言?
以下是我不喜欢它的原因:
PHP 的内置函数和库函数的命名不一致。 可预测的命名模式在任何设计中都很重要。
PHP 内置函数的参数顺序不一致,例如 array_map 与 array_filter,这在简单情况下很烦人,并引发各种意外行为或更糟糕的行为。
PHP 开发人员不断弃用内置函数和较低级别的功能。 一个很好的例子是他们弃用了函数的引用传递。 这给任何从事函数回调等工作的人带来了一场噩梦。
重新设计时缺乏考虑。 在许多情况下,上述弃用消除了为函数提供默认关键字值的能力。 他们在 PHP 5 中修复了这个问题,但在 PHP 4 中弃用了引用传递!
名称空间执行不佳(以前根本没有名称空间)。 现在名称空间已经存在,我们使用什么作为取消引用字符? 反斜杠! 该字符普遍用于转义,甚至在 PHP 中也是如此!
过于宽泛的隐式类型转换会导致错误。 我对隐式转换没有问题,例如从浮点到整数或再返回。 但是 PHP(我上次检查过)会很乐意尝试神奇地将数组转换为整数。
递归性能较差。 递归对于任何语言的写作来说都是一个非常重要的工具。 它可以使复杂的算法变得更加简单。 糟糕的支持是不可原谅的。
函数不区分大小写。 我不知道他们对此有何想法。 编程语言是一种明确地向计算机和代码阅读器指定行为的方法。 不区分大小写会带来很多歧义。
PHP 鼓励(实际上需要)处理与表示的耦合。 是的,您可以编写不这样做的 PHP,但实际上以不正确的方式(从健全的设计角度来看)编写代码更容易。
如果没有缓存,PHP 性能非常糟糕。 有人出售 PHP 的商业缓存产品吗? 哦,看,PHP 的设计者就是这么做的。
最糟糕的是,PHP 让人们相信设计 Web 应用程序很容易。 它确实使所涉及的大部分工作变得更加容易。 但事实是,设计一个既安全又高效的 Web 应用程序是一项非常困难的任务。
通过说服如此多的人开始编程,PHP 教会了整个程序员群体的坏习惯和坏设计。 它使他们能够访问他们不了解如何安全使用的功能。 这导致了 PHP 不安全的名声。
(但是,我会欣然承认 PHP 并不比任何其他 Web 编程语言更安全或更不安全。)
我对 PHP 缺少什么? 我看到一种有机生长、管理不善的语言正在滋生糟糕的程序员。
所以请说服我否则!
最受好评的答案
我会尽力回应您的每个要点
我对这个话题既爱又恨。 因为从本质上讲,这个问题是正确的。 为什么有些双字函数用下划线分隔,而有些则不是? 为什么 Needle 和 haystack 参数有时会交换参数签名中的位置? 这太荒谬了。 但归根结底……这真的很重要吗? 我的 IDE 带有智能感知和 php.net,只需点击一下浏览器即可,这没什么大不了的。 这是对 PHP 作为一种语言的负面影响吗? 是的。 它会妨碍我成为一名高效程序员的能力吗? 不。
就我个人而言,我认为这不是一个好点。 弃用对于一种语言的发展是必要的,尤其是像 PHP 这样有很多缺陷的语言。 PHP 因为“很容易成为一个糟糕的程序员*”而受到很多批评,但与此同时,当 PHP 小组试图从语言中删除愚蠢的结构(例如调用时间传递)时,他们也遇到了麻烦-参考。 消除呼叫时间传递参考是他们做过的最好的举措之一。 对于新手开发者来说,没有比这个“功能”更容易搬起石头砸自己脚的方法了。
我不认为普遍缺乏考虑,我认为你只是被这个特殊的变化刺痛了,嘴里留下了酸味。 语言的变化通常会提前几个月甚至几年就知道。 提供了从 4 到 5 的迁移指南,并且版本差异记录在手册中。 调用时传递引用是一个可怕的“功能”,并且不给开发人员任何通过其他方式无法获得的表达能力。 我很高兴它消失了(还有其他垃圾,比如魔术引语)
对此我五味杂陈。 我的一部分认为“谁在乎,无论如何,字符转义在字符串之外没有任何意义”,我的一部分认为“他们肯定可以使用更好的东西”。 但他们可以吗? 我不知道,我不是 Zend 解析器的开发人员。 PHP 在 5.3 之前根本没有命名空间,这是一个巨大的疏忽吗? 是的,一点没错。
我认为可以不同意 PHP 是如何做到这一点的,但不同意它使该语言变得“糟糕”。 但问我有多想坐在这个话题上争论弱类型和强类型。 (PS,完全不)郑重声明:当参数的类型很重要且无法通过强制解决时,PHP 将发出 E_WARNING 级别错误。
PHP 是一种用于 Web 的 DSL。 我已经全职从事这工作 8 年了,可能使用过 4 或 5 次递归,通常是为了某种类型的烦人的目录或 XML 遍历。 这并不是 Web 开发经常需要的模式。 我并不是在原谅性能缓慢,但这是一个学术问题,而不仅仅是一个生产问题。 如果您需要真正强大的递归性能,那么 PHP 已经不适合您了。
我完全100%同意这一点。
*嗯,这个话题听起来非常熟悉...
但说实话,我发现人们会抱怨一种语言绝对 100% 让你实现你想要的任何输出系统(仅 PHP 模板系统的庞大数量和风格就说明了这一点)到此) - 或者 - 跳过所有开销并直接输出。 这并不会让 PHP 变坏。 这是 PHP 优秀的部分原因。
您是指字节码缓存(如加速器)还是输出缓存?
如果是前者,那么我真的不知道我有多关心这个话题。 加速器是免费且易于运行的。 我们可以争论为什么它不是语言的一部分,但最终,我认为这并不重要。
如果您正在谈论输出缓存,那么我不知道该对您说什么。 任何具有大量流量的 Web 项目都需要缓存(例如种子播客 #27)。 这根本不是 PHP 特有的问题。
总之,我认为您从非常学术的角度认为 PHP 是一种“糟糕”的语言。 在您之前的文章中,您可能被像我这样使用 PHP 来“完成工作”的人否决了。
第二个最受好评的答案
您的所有批评(以及更多批评)都是有效的。 你被允许甚至被期望讨厌 PHP。
但是,话又说回来,它有一些好处:
最后,您可以通过编写用任何其他语言编写的优秀代码来克服许多(如果不是全部)缺点。 您可以用 PHP 编写可靠、安全且气味良好的代码,很多时候,它比许多替代方案运行得更快,并且更容易托管和扩展。
第三个最受好评的答案
简单的。 事实上,贫穷的程序员对他们的语言非常防御。 ;)
PHP 很容易学习,比其他替代方案容易得多,一旦您学会了它,就不会很明显 1) PHP 出了什么问题,2) 替代方案如何更好,以及 3) 如何切换到并学习,替代方案之一。
也许事实上,人们还有什么选择呢? 应用服务提供商? 它本身就存在很多问题,从无法在大多数网络服务器(Apache)上运行,到其本身的一些荒谬且过度设计的设计选择(Webforms?Viewstate?AJAX,您的异步“请求被拦截并运行依次?)
Ruby on Rails? 好吧,也许,除了有多少网络服务器再次支持它? 目前它并不容易接近。 而且速度很慢。
因此,也许 PHP 的“优势”确实在于不存在好的替代方案。
至少这就是为什么我尽可能远离所有网络编程的原因。 PHP 很糟糕,而且我也不太热衷于任何替代方案。
PHP 有很多基本问题,甚至一点都不好笑。 从缺乏 unicode 支持,到许多隐式类型转换(通常会导致意外的安全漏洞),到演示文稿和...其他所有内容的完全混合,或者到默认数据库模块(我上次检查)不使用参数化查询。 我们谈论的是一种为两件事而设计的语言:数据库访问和生成 HTML,而它在这两方面都很糟糕。
它只是一团糟,一种由没有资格或能力设计语言的人设计的语言。 ;)
As the question I linked was deleted, I'll place some of it here:
Question
I made a tongue-in-cheek comment in another question thread calling PHP a terrible language and it got down-voted like crazy. Apparently there are lots of people here who love PHP.
So I'm genuinely curious. What am I missing? What makes PHP a good language?
Here are my reasons for disliking it:
PHP has inconsistent naming of built-in and library functions. Predictable naming patterns are important in any design.
PHP has inconsistent parameter ordering of built-in functions, eg array_map vs. array_filter which is annoying in the simple cases and raises all sorts of unexpected behaviour or worse.
The PHP developers constantly deprecate built-in functions and lower-level functionality. A good example is when they deprecated pass-by-reference for functions. This created a nightmare for anyone doing, say, function callbacks.
A lack of consideration in redesign. The above deprecation eliminated the ability to, in many cases, provide default keyword values for functions. They fixed this in PHP 5, but they deprecated the pass-by-reference in PHP 4!
Poor execution of name spaces (formerly no name spaces at all). Now that name spaces exist, what do we use as the dereference character? Backslash! The character used universally for escaping, even in PHP!
Overly-broad implicit type conversion leads to bugs. I have no problem with implicit conversions of, say, float to integer or back again. But PHP (last I checked) will happily attempt to magically convert an array to an integer.
Poor recursion performance. Recursion is a fundamentally important tool for writing in any language; it can make complex algorithms far simpler. Poor support is inexcusable.
Functions are case insensitive. I have no idea what they were thinking on this one. A programming language is a way to specify behavior to both a computer and a reader of the code without ambiguity. Case insensitivity introduces much ambiguity.
PHP encourages (practically requires) a coupling of processing with presentation. Yes, you can write PHP that doesn't do so, but it's actually easier to write code in the incorrect (from a sound design perspective) manner.
PHP performance is abysmal without caching. Does anyone sell a commercial caching product for PHP? Oh, look, the designers of PHP do.
Worst of all, PHP convinces people that designing web applications is easy. And it does indeed make much of the effort involved much easier. But the fact is, designing a web application that is both secure and efficient is a very difficult task.
By convincing so many to take up programming, PHP has taught an entire subgroup of programmers bad habits and bad design. It's given them access to capabilities that they lack the understanding to use safely. This has led to PHP's reputation as being insecure.
(However, I will readily admit that PHP is no more or less secure than any other web programming language.)
What is it that I'm missing about PHP? I'm seeing an organically-grown, poorly-managed mess of a language that's spawning poor programmers.
So convince me otherwise!
Top Rated Answer
I'll take a stab at responding to each of your bullet points
I both love and hate this topic. Because at its core, this issue is correct. Why are some bi-word function split with an underscore, and some aren't? Why do needle and haystack parameters swap positions in the argument signature sometimes? It's ridiculous. But at the end of the day... does this really matter? My IDE with intellisense and php.net just a browser click away, this is just plain not that big of a deal. Is it a negative against PHP as a language? Yes. Does it hinder my ability to be an effective programmer? No.
Personally, I think this is not a good point. Deprecation is necessary to the evolution of a language, especially one that has as much kruft as PHP does. PHP gets a lot of flak for "making it easy to be a bad programmer*" but at the same time, the PHP group also gets in trouble when they try to remove stupid constructs from the language, such as call-time pass-by-reference. Eliminating call-time pass-by-reference was one of the best moves they ever made. There was no easier way for a novice developer to shoot themselves in the foot than with this "feature".
I don't think there's a general lack of consideration at all, I think you just got stung by this particular change and have been left with a sour taste in your mouth. Language changes are often known months if not years ahead of time. A migration guide was provided for the move from 4 to 5, and the version differences are documented in the manual. Call-time pass-by-reference was a horrible "feature" and doesn't give the developer any expressive power they can't get by other means. I'm glad it is gone (along with other crap like magic quotes)
I have mixed feelings about this. Part of me thinks "who cares, character escaping has no meaning outside of a string anyway", and part of me thinks "surely they could use something better". But could they? I don't know, I'm not a developer for the Zend parser. Is it a huge oversight that until 5.3 PHP never had namespaces at all? Yes, absolutely.
I think it's ok to disagree with how PHP does this, but disagree that it makes the language "bad". But ask me how much I want to sit in this topic and argue about weak vs strong typing. (P.S. I don't, at all) For the record: PHP will issue an E_WARNING level error when the type of an argument matters and cannot by solved by coercion.
PHP is a DSL for the web. I've been doing it full-time for 8 years and have maybe used recursion 4 or 5 times, usually for some type of annoying directory or XML traversal. It's just not a pattern that is needed for web development that often. I'm not excusing the slow performance, but this is an academic issue far more than it is a production issue. If you need really powerful recursive performance, PHP is already the wrong language for you.
I totally 100% agree with this.
*Hmmm, this topic sounds desperately familiar...
But seriously, I find it remarkable that people will complain about a language that will absolutely 100% let you implement any output system you want (the sheer volume and style of PHP templating systems alone speaks to this) - OR - skip all that overhead and just output directly. This does not make PHP bad at all. It's part of what makes PHP good.
Do you mean bytecode caching (like an accelerator), or output caching?
If the former, then I don't really know how much I care about this topic. Accelerators are free and easy to run. We could argue about why it isn't part of the language but in the end, I don't think it matters much.
If you are talking about output caching then I don't know what to say to you. ANY web project with significant traffic needs caching (seed podcast #27, for example). This is not a PHP-specific issue at all.
In summary, I think you consider PHP a "bad" language in a very academic fashion. And in your previous post you were probably voted down by people like me who use PHP to "get things done".
Second Top Rated Answer
All your criticisms (and some more) are valid. You are allowed and even expected to hate PHP.
But, then again, it has some benefits:
Finally, you can overcome many if not all the downsides by writing good code you'd write in any other language. You can write solid, secure and good smelling code in PHP, which many times will run faster and be easier to host and to scale than many alternatives.
Third Top Rated Answer
Simple. The fact that poor programmers get very defensive about their language. ;)
PHP is easy to learn, much easier than the alternatives, and once you've learned it, it's not exactly obvious 1) what's wrong with PHP, 2) how the alternatives are better, and 3) how to switch to, and learn, one of the alternatives.
And perhaps the fact that, well, what alternatives do people have? ASP? That has plenty of problems on its own, from being unable to run on the majority of webservers (Apache), to some ridiculous and overengineered design choices on its own (webforms? Viewstate? AJAX where your asynchronous" requests are intercepted and run sequentially?)
Ruby on Rails? Well, perhaps, except how many webservers support it again? It's not exactly easily approachable at the moment. And it's slow.
So perhaps PHP's "strength" is really that no good alternative exists.
At least this is why I stay away from all web programming when at all possible. PHP sucks, and I'm not too keen on any of the alternatives either.
PHP has so many fundamental problems that it's not even funny. From the lack of unicode support, to the many implicit type conversions which often lead to unexpected security holes, to the complete mixing of presentation and... everything else, or to the default database module which doesn't (last I checked) use parametrized queries. We're talking about a language made for two things, database access and generating HTML, and which is terrible at both.
It's just a nasty mess, a language designed by people who aren't qualified, or able, to design a language. ;)
对我来说,最严重的 PHP 罪过是表示与业务逻辑的耦合。 这并不是说你不能以更好的方式编写它,而是它不鼓励你这样做,甚至鼓励你不要这样做。
PHP 站点也存在大量安全漏洞。 我无法证明这是不成比例的(毕竟很多网站都是用 PHP 编写的),但我怀疑确实如此。 如果我是对的,那么由于安全漏洞是一类错误,我怀疑 PHP 网站总体上也更容易出现错误。
(顺便说一句,我认为指出一些大型网站并说他们设法用 PHP 做到这一点并不是反对这一点的论点。这有点像说香烟不会致癌,因为你的隔壁邻居抽烟并活到了 100 岁。)
For me the worst PHP sin is coupling of presentation with business logic. It's not that you can't write it in a better way, but it doesn't encourage you to, and if anything it encourages you not to.
A large number of security vulnerabilities are associated with PHP sites, too. I can't prove it is disproportionate (after all a lot of sites are written in PHP), but I suspect it is. If I'm right, then since security vulnerabilities are a class of bug, I suspect PHP sites tend to be more buggy on the whole also.
(I don't think that pointing at a few large sites and saying they managed to do it in PHP is any argument against this, by the way. It's a bit like saying that cigarettes don't cause cancer because your next door neighbour smoked and lived to be 100.)
看看这个类似的问题 -
PHP 可以像 Java 一样处理企业级网站
回顾 - Facebook、维基百科、Yahoo.com、Digg、Flickr 和许多其他大型网站都在 PHP 上运行。 如果您曾经接近制作出这样的作品,您仍然可以放心使用 PHP 来实现这一目标。
您的应用程序的可维护性、可扩展性、可靠性、安全性和性能完全取决于您,并且与语言无关。 不过,PHP 拥有非常方便的构建 Web 应用程序的工具。
Check out this similar question -
Can PHP handle enterprise level sites as well as Java
Recapping - Facebook, Wikipedia, Yahoo.com, Digg, Flickr and many other giant sites are running on PHP. If you ever come close to making something of that caliber, you can still rest assured you can get there with PHP.
How maintainable, extendable, reliable, secure and performant your applications will be is entirely up to you and is language-agnostic. In favor of PHP though, it has very convenient tools for building web applications.
对我来说,谈论大型甚至巨大的项目时,它(主要)可以归结为一个词:依赖。
脚本语言的问题就像世界上所有事物一样:最大的优点同时也是最大的缺点。
最大的优点是可以免费且快速地编写代码。 只需编写一个脚本即可实现其目的。 不需要冗长的内容,只需编写代码即可。
在某种程度上,最大的缺点是检查该脚本是否不会干扰其他脚本。 或者更好:更改其他人依赖的旧脚本。 您确定所有依赖项都按照您的预期运行吗?
对于“正常”网页生成来说,情况并非如此,无论“正常”在这里意味着什么。 但我们的产品依赖于大约 50 万行源代码,并且为客户端进行的定制也包含额外的 10 万行代码。 我非常高兴编译器会检查所有依赖项,并在我做错事情时发出警告/错误(例如,在这里说最低级别,错误输入变量或方法调用)。
我认为这一点以及其他语言本质上提供了更易于使用的“企业”功能(即用于“银行用途”的应用程序服务器)这一事实可以归结为为什么许多人看不到 PHP 的规模(或者更好:巨大)项目。
For me, and talking about large or even huge projects, it (primarily) boils down to one word: Dependencies.
The problem with a scripting language is like in every thing in the world: The greatest advantage is the greatest disadvantage at the same time.
The greatest advantage is to code free and fast. Just write a script and it will server it's purpose. No verbosity needed, simply code.
The greatest disadvantage is, in a way, to check if this script is not disturbing other scripts. Or better: Change an old script others are relying on. Are you sure that all dependencies do work out as you desired?
This is not true for "normal" web page generation, whatever normal means here. But we have a product relying on some 500k lines of source code, with customizations for clients consisting of an additional 100k lines of code, too. And I am deadly glad that a compiler checks all dependencies and warns/errors me in case I did something wrong (like, speaking lowest level here, misstyping a variable or method call).
I think this and the fact that other languages provide more simple-to-use "enterprisy" features by their nature (i.e. application servers for "bank usages") boils it down why many do not see PHP in large (or better: huge) projects.
我们公司使用 PHP 运行多个大型网站,并且没有遇到与该语言相关的问题。
Our company runs several large-ish web sites using PHP, and have had no problems that were related to the language.
PHP 语言的构建有些地方对我来说还不够好。
例如,函数的名称。 使用多种方法来用一种语言命名函数并不是最佳实践。 下划线(函数名)、单词粘在一起(函数名)等的混合。我的意思是,这真的是一团糟。 有太多的函数非常相似或做相同的事情,但它们的名称却很混乱。 这不是好的编程语言的特征。
在大型部署中,语言必须足够简单且易于编写。 PHP 省略的一些东西,比如变量类型的声明,会变得非常难以理解和处理。
另一点是不断添加功能并取消一些其他功能。 假设 PHP 5 中添加 OOP 会让程序员的事情变得更容易,但是向后兼容性的考虑呢?
这种编程语言之所以如此,主要原因在于它的起源:个人主页。 它不是为大型部署而设计的。
我知道人们正在付出巨大的努力来使这种语言成为企业级语言,并且就我个人而言,我正在等待一种足够好的开源服务器端编程语言; 但直到这一天到来之前,桥下将会流很多水。
There's something about the construction of the PHP language that is not good enough for me.
For example, the name of the functions. It is a non best practice use a several ways to name a function in one language. The mixture between underscores (function_name), words stick together (functionname), etc.I mean, this is really a mess. There are too many functions that are very similar or do the same things, but their names are so confusing. This is not characteristic of a good programming language.
In large deployments, the language must be enough easy and specific to write. Something that PHP omits like the declaration of variable types, become very difficult to understand and deal with later.
Other point is the constant addition of features and canceling of some other. It supposes that the addition of OOP in PHP 5 gonna make the things easier for programmers, but what about the considerations of back compatibility?
The principal reason that this programming language is like it is, is due to its origins: Personal Home Page. It was not designed for large deployments.
I know that there are great efforts to make this language an enterprise-weight language, and personally, I'm waiting for a good enough open source server-side programming language; but until this day comes, it's gonna run a lot of water under the bridge.
这些都是很好的答案。
我是个新手。 我只编码了 5 年,但我直接支持和管理 85 个大大小小的网站,我会告诉你,网站关闭一天而被起诉的可能性将极大地激发你的学习欲望如何编写更好的代码。
很高兴听到成熟的开发人员分享他们对此事的想法。 我不认为 PHP 是最好的,但看来我对“最佳实践”的投资得到了很好的回报。
感谢大家!
These are all good answers.
I was a newbie. I've only been coding for 5 years but I directly support and manage 85 small to large websites and I'll tell you what, the potential to get sued by having a website down for a day will contribute a lot to your desire to learn how to and make better code.
It's nice to hear established developers share their thoughts on this matter. I don't think PHP is the best, but it seems my investment in "best practices" is well served.
Thanks everyone!
请参阅:http://www.ukuug.org/events/linux2002/papers /html/php/
See: http://www.ukuug.org/events/linux2002/papers/html/php/