Câu hỏi phỏng vấn PHP
Câu hỏi

Explain the difference between shell_exec() and exec()

Câu trả lời

Certainly! Here's a detailed explanation of the difference between shell_exec() and exec() in PHP:

shell_exec()

  • Purpose: shell_exec() is used to execute a command via the shell and return the complete output as a string.
  • Return Value: It returns the entire output of the command as a single string. If the command fails or produces no output, it returns NULL.
  • Usage: This function is useful when you need to capture the full output of a command for further processing or display.

Example:

php Copy
$output = shell_exec('ls -l');
echo "<pre>$output</pre>";

In this example, the ls -l command lists the files in the current directory in long format, and the output is captured and displayed.

exec()

  • Purpose: exec() is used to execute a command via the shell, but it provides more control over the output and return status.
  • Return Value: It returns the last line of the command output as a string. Additionally, it can populate an array with the complete output and set a variable with the return status of the command.
  • Usage: This function is useful when you need to capture the last line of the output, or when you need to capture the entire output in an array and check the command's return status...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Are Parent constructors called implicitly inside a class constructor?

middle

Explain function call by reference

middle

What is the difference between using self and $this ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào