Home  >  Article  >  Backend Development  >  Is there anything that Python can do that PHP can't do?

Is there anything that Python can do that PHP can't do?

PHP中文网
PHP中文网Original
2016-08-15 18:01:471579browse

What can Python do that PHP can’t do?

Reply content:

I saw that the python programmer named "Spirit Sword" was talking nonsense. Not only in the answers, but also in the comments, he used his expired junior PHP qualifications to talk nonsense, and also came out to give him a few slaps. .

First of all, someone has already mentioned it below, dear, have you heard of "Turing Completeness"? If you haven’t heard of it, you can go to Wikipedia first (zh.wikipedia.org/wiki/%) Give it a try. Both PHP and Python are Turing-complete languages, so there is nothing that one can do that the other cannot.

Now that the questions have been answered, it’s time for the promised slap in the face, that is, let’s take a look at what “Spirit Sword” said about the so-called things that Python can do but PHP cannot do (to avoid repetition, “ I won’t repeat the parts that have been refuted by "Gutter". Some of the weird remarks are taken from his comments on "Spirit Sword"):
1. "When I originally wrote PHP, I mainly used curl. This is C library. It’s not that it’s bad, but if you use this library, some important functions will not work. For example, I want to read a URL, and then process the return value as a stream. Read part of it and process part of it: Please read the manual. PHP: CURLOPT_FILE in curl_setopt, "value should be a stream resource (using fopen(), for example) for the following values ​​of the The option parameter clearly states that streams are supported.
In addition, it is said that you also checked out a lot of third-party libraries? I don’t know if you have seen guzzle (GitHub - guzzle/guzzle: Guzzle, an extensible PHP HTTP client), a library with 7000+ stars.
2. "It is best that the returned stream can be processed uniformly like a file. You can directly use methods such as outputStream to write this stream to other languages. Basically, other languages ​​​​support such a function, but PHP does not." Same as above, as mentioned above Yes, you have fopened it. Of course, it can be processed uniformly like files
3. "Others, such as automatically reusing HTTP/1.1 connections, are not possible." Nonsense, you are talking about the web server. Go to apache/nginx. Of course, if you insist on using PHP as a web server, it is not impossible. Swoole is one, but if you think it is an extension written in C, it does not count. Then you can go take a look at Workerman (What? This also uses the C library, so it doesn’t count? Come on, it’s too much if you don’t allow the system’s own libraries to be used).
4. "Operation and maintenance just complained that the front-end nginx configuration of web services in other languages ​​​​is all unified. Only PHP has written a lot of rewrite. Just because PHP does not support binding handlers according to URL matching rules: Who is to blame for your poor business and a lot of rewrites? Mainstream frameworks are now basically single-entry designs that are rewritten into index.php and then distributed and processed internally
5. "You always think that PHP can do this or that. I think this is entirely because of what you want to achieve. The business is too simple and I have never seen a truly exquisite design. 』: In the same way, if you have too little ability and too little knowledge, PHP can also realize those so-called exquisite designs.
6. "Only your PHP is beautifully designed and not easy to use, because the syntax is too rigid, so you can only write noodle code, read other languages ​​​​such as Python and Java (Java is much better than you), and see How they use OOP to abstract business logic, how they use design patterns, how they manage business functions in a modular way, and how they provide plug-in extensions are all things that none of your PHPs can do. 』: PHP: Introduction PHP5 (more than 10 years ago) has provided OOP since the beginning. As for abstracting business logic, using design patterns, and modularizing plug-ins, aren’t they all matters of the program? Is it your fault of language for your poor ability?
7. "I have been writing PHP on SAE (Sina Cloud Service) for at least three years. During the peak period of my application, I crashed SAE (using fsockopen). I encountered all the most disgusting problems in PHP. been. 』To be fair, is that SAE garbage monster php? I have experienced crashes more than once. How can I blame PHP for changing so many things behind my back? At that time, I recommended SAE and Sina Cloud Store (it seems to be called this name, it is SAE without written restrictions) to the boss. As a result, various deployments were made. What a pitfall
8. "Only your PHP needs to be distinguished. Third-party extensions can only be copied into the website directory. There is no package management function and you have to be careful about namespace conflicts. Isn't it cool?" It takes a lot of effort to install the C extension and you have to change the configuration file. Isn’t it cool? 』: For PHP package management, please refer to Composer The namespace conflict has also been resolved. As for C extensions, please refer to pecl
9. "But do you really want a language that searches the local directory every time you use a class?": Please read psr-0, psr-4 and composer's autoload
10 "The autoloading mechanism relies on finding files whose names match the class names in the local directory and then loading them, right?": You can also implement PHP yourself without following the specifications: spl_autoload Then do whatever you feel comfortable with .
11. "Namespace conflict": PHP: Using namespaces: Aliasing/Importing
12. "One of the major supports for Java's Web is spring. Because of spring, we can build large-scale, plug-in, and usable designs Model Web, PHP can't do it, this is already a death sentence, which means that PHP can never develop Web applications on the scale of Java. 』: Let me guess what you want to say is IOC? Many frameworks have also implemented this, such as Service Container,
By the way, I wonder how small Facebook is in your eyes?



There are too many flaws so I won’t go into details. I want to explain some of the most basic things:
First of all, php is written in C. Is there any problem with this? According to your logic, only those that can complete bootstrapping can be called languages?
As for loading it every time, this is determined by the way it works with apache/nginx. To translate your question more professionally, can PHP implement web server by itself? The answer is yes. First of all, php -S comes with one. In addition, you can refer to the projects swoole (c implementation), workerman (php implementation), etc.


Finally, if you don’t know php and you still have to pretend to be proficient in it, it’s really hard on you. Does it hurt when so many people slap you in the face? Many people discuss python and php from a language level. My ability is limited and I can only discuss it from the perspective of resource ecology.

Let’s take a look at two awesome ones.

ziadoz/awesome-php

vinta/awesome-python

It is not difficult to find that php is mainly born for web development. And Python covers many fields, in addition to the web, there are many:

  • Big data processing

  • Data visualization

  • Machine learning

  • Embedded development

  • Big software api

  • desktop application


Thanks to the relationship between CPython and C, many software have python API interfaces. As for efficiency, using python for data processing is definitely inseparable from numpy and pandas. Of course, there is nothing wrong with using PHP if you want to quickly make a webapp. After all, it is the best language.

Python Note 1: Simple Introduction and CommentsZhihu is about to form a situation where people are proud to write python and ashamed to write php. It is really heartbreaking. Is it shameful to write php? Praising python and discrediting php is about to become politically correct on Zhihu.
Nowadays, there is a question about whether python can use php or not. Although everyone is Turing equivalent, can php also be considered a language?
Python can be used to install B, but php cannot. In Zhihu, phper is usually beaten by pythoners.
Python can have two versions 2 and 3 for you to choose from at any time, but php cannot.
Python can also do this: a = [1,2,3] Calculate the length len(a), sort, a.sort(), it simply absorbs both C series procedural formulas and object-oriented, it is simply touching, PHP is No, it's all ugly procedural calls.
Python’s indentation is simply a blessing for those who are obsessed with code. There is no way to write ugly code.

The above is just nonsense. I only know a little bit of php and ruby, but I don’t know python. But I really feel that python is still a good language, and there is also List comprehension similar to Haskell. I can’t help it anymore, that Python coder from Lingjian used to look at your stupid answers to the question about hacking PHP. It’s okay to hack PHP and I often hack it, but your answer about PHP is just nonsense and you pretend to understand. I’m going to slap you in the face with every word of your answer.

1. "Poor multi-threading support is just one of them." Let's put it this way, among the more mainstream scripting languages ​​​​such as php, ruby, python, etc., php is the best in terms of multi-threading support. py and ruby ​​support multi-threading by cutting corners and adding global locks to create fake multi-threading. PHP itself has transformed the language core into thread-safe and non-thread-safe versions. PHP's multi-threading is truly multi-threading. In fact, as a high-level language, scripting language is not robust to multi-threading, so I basically don’t use multi-threading features. If I want to use multi-threaded tasks, I still tend to use Java/C++ statically typed languages.
2. "For example, if byte streams, multi-byte strings, and unicode strings are not distinguished, when you need to write unicode, utf-8, and gbk strings at the same time in a PHP file, you must use hexadecimal escape. This What a crushing experience it is.” How many people have experienced coding disasters using Python without being disgusted? I basically never worry about coding issues when using PHP. Why don't you use py3? Yes, py3's stupid incompatibility with py2 has brought so much trouble to the third-party library community. Only Ubuntu16.04 dares to upgrade to py3. I'm sorry that most of the production environments centos6 still have py2.7.
3. "For example, if there are multiple return values, Python can return a tuple and then use simple syntax (a, b = my_func()) to restore the tuple content to different variables. PHP can only return an array, and then it is ugly to write several lines. Code. "php can not only return arrays, but also objects, etc., but I believe you mean that php does not have syntactic sugar like py. I don't care much about syntax sugar, so I only check it when I'm using it. The feature you mentioned seems to be available in PHP: $my_array = array("Dog","Cat","Horse");list($a, $b, $c) = $my_array; But that doesn't matter at all.
4. "PHP can survive to this day mainly because it is the first module that can adjust C, so it relies on C programmers to continue step by step. The same is true for swoole;" PHP writes CPU-intensive functions into C modules just for performance. This What's there to be ashamed of? The best feature of php7 is also to improve performance while being as compatible as possible: pages.zend.com/rs/zendt. As far as I can tell, the php language and community are developing well.
5. “Secondly, because PHP programmers are generally of low quality and have a very narrow range of requirements for writing code, they have never encountered the hard boundaries of this language. Basically, if you write Python once or twice, you will not want to start again. Use PHP again. "You are wrong to use map guns like this. Each language has its usage scenarios. I used to write a lot of py, but I think PHP is more useful for web-related things.
6. “Wait until someone uses pure PHP to write a cloud computing management platform like OpenStack, a small and exquisite Web framework like Flask, a simple and easy-to-use crawler framework like Scrapy, or even an HTTP client like requests. Instead of dragging libcurl to make do with it today, let’s talk about what PHP can’t do. “I haven’t used OpenStack and it’s inconvenient to comment. The team here has been using Flask and refactored it to Java after encountering performance problems. Scapy. If you open your mouth and make requests, I will slap you in the face with this 2 today. As a crawler framework, Scrapy basically writes a few configuration files and you can run it. However, the essence of a crawler is to open an httpclient to retrieve HTML+DOM operations to extract data. Scrapy's encapsulation is very powerful and is suitable for beginners to just change a few parameters and start running. , but scales particularly poorly. When the py coders who work on crawlers here want to complete crawlers that require special customization, they directly open requests and come back to beautifulsoup to extract data. The requests httpclient is very easy to use. PHP's httpclient is not limited to libcurl. For larger libraries, guzzle is also very easy to use and has great asynchronous support. When I use guzzle, I can use libevent's event library to open only one guzzle httpclient in a single process to concurrently crawl 100 websites asynchronously, but requests do not have asynchronous support.

I said all this just to slap you in the face, the above



--------------------------------- ----------------
After reading Coldwings’s relatively competent answer to me, I suddenly became interested. Here is my response:

1. Regarding the encoding issue, I believe it has been solved. The discussion was clear enough, and I think what you said makes sense. I won’t respond to the emotional stuff here.
2. I have researched asynchronous concurrency solutions under py for a long time. grequests (GitHub - kennethreitz/grequests: Requests + Gevent =<3) is a very thin package of gevent. It has many stars but less than 150 lines of code. Not very useful actually.
3. Coroutine essentially writes asynchronous in a synchronous way, so coroutine does not mean that it is better than asynchronous in terms of concurrency.
4. Scrapy is not the only one that can schedule phantomjs (you can integrate phantomjs into any crawler). In fact, I have some experience in driving phantomjs concurrently under php (a single machine i7 can drive 80 phantomjs instances concurrently, including concurrent caching, Corresponding handling of various pitfalls such as proxies, resource leaks, and headless development difficulties), I believe that using py to drive phantomjs is similar. It depends on what you use.
5. When it comes to crawlers, what you call distributed refers to concurrent crawling by multiple machines. I use redis more often, but of course I don’t have many machines. I used PHP to write a multi-node concurrent crawler solution based on redis. There is a crawler with 2 PCs that can concurrently run up to 150 phantomjs + 150 guzzle httpclients. For multi-crawler collaboration and task distribution, I recommend redis over a dedicated message queue, because redis's single-machine 10w qps not only satisfies crawler concurrency, but also has 5 very easy-to-use data structures for scheduling of crawlers, Status viewing and dynamic parameter setting are very helpful.
6. The concurrency of my crawler is 100, which is HTTP concurrency, not TCP concurrency. I use PHP to run asynchronously and concurrently under a single process and single httpclient under low CPU and low memory, which is full of small water pipes. I think the pros and cons of the thread pool solution and the asynchronous concurrency solution have been discussed clearly enough (in addition, I really think my single httpclient asynchronous multi-concurrency crawler solution based on guzzle is very good, I am very proud of this. If you If you are interested, you can read my two blogs discussing this issue: Talking about crawler concurrency issues and experience using redis + understanding of high-concurrency httpclient ).
7. I have also investigated python’s concurrent http solution before (What is the fastest way to send 100,000 HTTP requests in Python?), which also includes the asynchronous httpclient of grequests and tornado you mentioned. Tornado's asynchronous httpclient is based on libcurl (tornado.httpclient) and does not support concurrency. PHP's guzzle defaults to libcurl, but you can choose other event libraries such as libevent. Guzzle supports asynchronous concurrency. (I have been looking for such httpclient for a while under py and java).

Finally, I just saw that the Python coder from Lingjian who hacked PHP was really wrong in every word he said (I also saw from his previous answers that he was too prejudiced against PHP, and he didn’t understand PHP at all) I know PHP, it’s really annoying to mislead others like this), I just jumped out and slapped you in the face because I couldn’t stand it. I believe that each language has its most suitable scenarios, and I thoroughly enjoy communicating with others about technical things. Thanks for the invitation. As far as I know, there is no such thing on the web. The multi-threaded PHP mentioned above can be completed using swoole, and it is not impossible.
One of the best aspects of PHP’s design is to extend this mechanism. Even if py has other new features and functions on the web in the future, PHP masters who know C language can also use C to create an extension for PHP to use.
In short, every language has its own meaning. Don’t compare. If you have the time for comparison, you might as well write a few more pieces of code. Python can write code without dollar signs.
PHP cannot. Thank you! The two languages ​​face different fields. If they are compared horizontally, it will inevitably lead to meaningless disputes.
Before the birth of the language PHP, there was already a red ocean in the WEB area. If you follow what the python master above said, there is no need for PHP to appear at all. However, PHP appeared and continued to grow, now occupying most of the web development market.
The emergence of everything has its inevitability, and the future Internet technology field will increasingly tend to be implemented quickly. I believe that if it were not for the emergence of Dz!, BBS would not have developed so rapidly in China in the past few years. It can be seen that PHP has made a huge contribution to the development of the Internet in China and around the world.
Many people are saying that Zuckerberg regrets using PHP, but if he had not chosen PHP at that time, FB might not have dominated. When Bill Gates launched the Windows operating system, it was actually not perfect. In order to cultivate user habits earlier, he would rather put software that he thought was imperfect into the market. If he wants to pursue perfection before launching it, maybe even if it is perfect by then, not so many people will use it.
Officially, this fleeting business opportunity has created the glory of PHP. Launch a usable application first, then spend time improving it. Even using C will get a lot of support because the market has already applied for the feasibility of the application.
So PHP and PY cannot be compared horizontally. As for what PY can do that PHP cannot do, my answer is that projects that already have ample time and sufficient feasibility are not suitable for PHP, such as tailor-made customization. ERP. There is something that Php can't do, I'm curious! In fact, there is nothing that only py can do that php cannot do.

Personal taste plays a certain role in language selection. If the person chosen at that time happens to have done some very influential work, then inertia will be formed, and later on he will be unable to stop.

A large part of py's applications are numerical calculations, and there are many libraries. Some people have cited some reasons before: why those who engage in numerical calculations prefer python to other languages. One of the reasons is that the syntax is more acceptable to these scientists.

So if you have to say that py can do something that php cannot: It may be that the syntax of py is more attractive to users in other industries.

10 Reasons Python Rocks for Research (And a Few Reasons it Doesn’t)

Put a link for reference. The question is too subjective
Please define exactly what “cannot be done”
c/c++ can be used for web development, bash can also write scientific calculations, and even node.js can be extended to embedded development
There are only relative and qualitative "not suitable for doing", there is no "cannot be done"

The above is what Python can do, but PHP cannot do? For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn