search
HomeBackend DevelopmentPHP ProblemCan php do the Internet of Things?

Is PHP not suitable for the Internet of Things server?

Can php do the Internet of Things?

In traditional thinking, people often tell you that PHP is not suitable for IoT servers and ask you to change to Java or node. , go and other languages, yes, yes, 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 at all. (Recommended learning: PHP video tutorial)

For example, when you want to implement a TCP server, you may need to write code with the following principle:

for ($i = 0;$i <= 1;$i++){
    $pid = pcntl_fork();
    if($pid){
        if($i == 0){
            $server = stream_socket_server("tcp://127.0.0.1:9501", $errno, $errstr, STREAM_SERVER_BIND);
        }else if($i == 1){
            $tickTime = time()+3600;
            while (1){
                usleep(1);
                if($tickTime == time()){
                    //do my tick func
                }
            }
        }
    }}

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 is a PHP multi-process framework that highly encapsulates the above code principles and helps you focus on implementing code logic. Therefore, it is said that PHP Doing the Internet of Things from time to time is actually a fallacy.

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. That is correct. 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, which is the coroutine of

Swoole4.x.

The above is the detailed content of Can php do the Internet of Things?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.