Many php programmers switch to go because of the current environment. Today I bring you a new perspective - “go is a better php”.

It starts with the article PHP showing its maturity in release 7.4. The article says that PHP is maturing from version 7.4 and lists the following advantages of PHP.

Complete object-oriented programming support, mainly including

  • namespace to handle naming conflicts
  • interface separates implementation from interface
  • traits code mixin support
  • type system restricts the types of function parameters, return values, and class members

A comprehensive dependency management system, which is mainly implemented by composer.

Support for FFI (foreign function interface), which makes it easy to interact with various C libraries (e.g. TensorFlow) and greatly extends This greatly extends the capabilities of PHP.

More secure , mainly by introducing a more modern and more secure cryptography library libsodium.

Increasing performance , PHP 7.4 is about 3 times faster than PHP 5.6 and almost 18% faster than PHP 7.0. The main performance improvement comes from PHP7.0’s zval refactoring. PHP 8.0 will introduce JIT technology, which will provide even greater improvements.

As an old PHP programmer, I think this article only talks about the advantages of PHP, and ignores the disadvantages of PHP.

  1. No support for generics. Programmers can’t restrict the member types of PHP’s arrays.
  2. Runtime type checking. PHP’s parameter type checking is done during code execution, which reduces execution efficiency.
  3. PHP virtual machine is a C while loop that reads and executes. If you want to run a full multi-core, you can only use multiple processes or both multi-threads, which have synchronization problems, and multiple processes have IPC problems.
  4. PHP itself and extensions are developed in C. To understand PHP in depth, writing PHP code alone is not enough. This increases the threshold for improving PHP to some extent.

The go language solves exactly these problems

  • Support for basic generic types slice and map both support specified types
  • compile-time type checking, no runtime overhead
  • Thread-based implementation of goroutine, with inherent multicore support
  • Use go language bootstrap, compiler and runtime itself is developed by go language plus a small amount of assembly language, as long as you are familiar with go language, you can study compiler and runtime code in depth
  • Type derivation support, development experience similar to dynamic languages like php

It is true that go loses a lot of performance compared to c, but it can basically beat php in all aspects compared to it. If you have to say that go has something inferior to php, it is only the community openness of this one. go is developed by google, while php is developed by the community. But this one has little impact on small companies or developers in general.

I played with PHP in college and have been using it as my main development language since I started working. I used to think about developing everything in PHP, and I even built a vim development plugin for php. But PHP is not a general-purpose language after all, and the most suitable application scenario is writing templates (which are constantly replaced by node). In the end, I chose go when starting a new project. Of course, go is far more than just a better PHP, but using go as PHP gives you a different understanding of PHP and go.