为什么在PHP与C中编写服务器端脚本更容易(所有与Web相关的函数都有库)?

If PHP is written in C, what exactly would be the difference between writing a server side script in PHP vs C, esp if I was using a web framework for C that could handle mysql connections, SESSIONS, etc.?

PHP programs uses more memory and run slower, but has a well-suited, large library. Writing C is relatively tedious, and will take a longer time, but they will be fast and efficient. How much faster and efficient, depends on the program.

If your PHP program doesn't have a performance bottleneck due to a piece of PHP code (because it's interpreted code), I can suggest you to go on with PHP.

Alternatively, by the way, you may also want to try Go.

That is like asking why you wouldn't write a server side script in assembly. PHP is a higher level language designed to make writing those scripts easier. The difference would be the time and effort required to get the job done... And to make it run on a web server.

Examples:

  • memory management
  • built in globals like $_REQUEST, $_SERVER, etc
  • built in session mechanics
  • built in handling of HTTP headers (reading and writing for responses)

You've asked an extremely general question, so here's an extremely general answer

PHP - Derived from perl, used mainly as a web programming language. Generally interpreted, although some projects such as HipHop compile it. It has many features designed specifically for generating dynamic web content. Other things are available as library.

C - Derived from Algol 68. Used to program everything you can imagine, at least at one point. Many large systems (such as the Linux Kernel) are written entirely in c. C has very few features in the language, even basic string processing requires the use of the C standard libraries.

One unexpected similarity between C and some versions of PHP is the single, global namespace.

You can write a website purely in C. It has been done before, and could be done again. However, there is very little reason to do so. At most, it would be worth writing a small section of your website (or even just a few performance critical operations) in C and the remainder in an interpreted language like PHP, Python, etc.

You can absolutely use C as your server side application. That being said, please note that PHP is a high level language which will result in better managed code and shorter developing times.

Google for instance, uses C++ for speed critical systems in their infrastructure. However, if you are not aiming for something that is performance hungry, probably you are better off with a high level language like PHP.

PHP is written in C. This means that, if you want to write a server side application in C, you have to do all the extra work yourself. (ie. memory management, session mechanics, etc...)