If you're trying to use exec in a script that uses signal SIGCHLD, (i.e. pcntl_signal(SIGCHLD,'sigHandler');) it will return -1 as the exit code of the command (although output is correct!). To resolve this remove the signal handler and add it again after exec. Code will be something like this:
(more codes, functions, classes, etc)
// Now executing the command via exec
// Clear the signal
// Execute the command
// Set the signal back to our handler
// At this point we have correct value of $retval.
Same solution can apply to system and passthru as well.