Isn’t PHP suitable for the Internet of Things server?
In traditional thinking, people often tell you that PHP is not suitable for IoT servers. Let you switch to other languages such as Java, node, go, etc. Yes, it is true that PHP in the traditional sense is indeed difficult to make an Internet of Things server because it is so lame. Of course, this does not mean that it cannot be done completely.
For example, when you want to implement a TCP server, you may need to write code with the following principle: (Recommended learning: PHP programming from entry to proficiency )
for ($i = 0;$i <= 1;$i++){<br/> $pid = pcntl_fork();<br/> if($pid){<br/> if($i == 0){<br/> $server = stream_socket_server("tcp://127.0.0.1:9501", $errno, $errstr, STREAM_SERVER_BIND);<br/> }else if($i == 1){<br/> $tickTime = time()+3600;<br/> while (1){<br/> usleep(1);<br/> if($tickTime == time()){<br/> //do my tick func<br/> }<br/> }<br/> }<br/> }}<br/>
The meaning of the above code is equivalent to creating a TCP server in one process, and doing time detection in an infinite loop in another process, thereby realizing the timer logic.
This looks really lame, and for PHPer, whose programming foundation is generally weak, it is really difficult to maintain.
Of course, at this time, some people will say, isn’t there Workerman? Yes, there is indeed Workerman. Workerman highly encapsulates the above code principles and helps you focus on implementation. The code logic is a PHP multi-process framework, so it is a fallacy to say that PHP does not always do the Internet of Things.
Of course, some people may say at this time that the go language has coroutines. When you use Workerman to block database calls, the efficiency will be very poor and it will be difficult to achieve high concurrency. So Yes, but in fact, we can use multiple processes as much as possible to make up for this shortcoming, that is, a heap machine.
Of course, if you really want to spend every penny, it doesn't matter. At this time, we can come up with our killer weapon, that is the coroutine of Swoole4.x.
Actor Model
What is Actor? Simply put, Actor is a highly abstracted concurrency model. Each Actor instance The memory spaces are isolated from each other to reduce the difficulty of user programming and maintenance.
The above is the detailed content of Will php be used for the Internet of Things?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
