How the PHP Function Proc_Close Works
In PHP you can interact with external processes by passing data to them via pipes. These pipes are opened with proc_open() which returns a handle to the process, and when you are done, you can close them with proc_close(). It will also return the process's termination code if it is known (return value depends on whether --enable-sigchild is enabled when compiling PHP).
In this article, we will explore how proc_close works. We will start with a basic example using the sys.stdin and sys.stdout which are the standard input and output channels of the OS. Then, we will run a shell command from inside the PHP script and pipe that output to the text editor Notepad.
The function name is the first parameter and the remaining parameters are optional. The second argument is an integer which represents the process's priority. The higher the number, the more important the process is. The third argument is an indexed array which specifies the descriptor numbers for which PHP will pass data to the process. Normally this is limited to 0 for stdin, 1 for stdout and 2 for stderr, but it is possible to pass more than these (for example a passphrase to a cryptographic program) which may need other descriptors.
In PHP 7, it is possible to declare a type for the return function. This will make sure that only the correct type of object is returned. This can help to prevent issues such as a NullPointerException when using the return function.