9951 explained code solutions for 126 technologies


php-pcntlHow to use pcntl_wexitstatus in PHP?


PCNTL_WEXITSTATUS is a PHP function used to retrieve the exit status of a child process. It is part of the pcntl extension.

Example

<?php
$pid = pcntl_fork();
if ($pid == -1) {
    die('could not fork');
} else if ($pid) {
    // parent
    $status = null;
    pcntl_waitpid($pid, $status);
    $exitStatus = pcntl_wexitstatus($status);
    echo "Child exited with status $exitStatus\n";
} else {
    // child
    exit(3);
}

Output example

Child exited with status 3

Code explanation

  • pcntl_fork(): creates a child process
  • pcntl_waitpid($pid, $status): waits for the child process to finish
  • pcntl_wexitstatus($status): retrieves the exit status of the child process

Helpful links

Edit this code on GitHub