Saturday, November 16, 2024
Google search engine
HomeLanguagesPHP | shell_exec() vs exec() Function

PHP | shell_exec() vs exec() Function

shell_exec() Function

The shell_exec() function is an inbuilt function in PHP which is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails return NULL and the values are not reliable for error checking.

Syntax:

string shell_exec( $cmd )

Parameters: This function accepts single parameter $cmd which is used to hold the command that will be executed.

Return Value: This function returns the executed command or NULL if an error occurred.

Note: This function is disabled when PHP is running in safe mode.

Example:




<?php
  
// Use ls command to shell_exec
// function
$output = shell_exec('ls');
  
// Display the list of all file
// and directory
echo "<pre>$output</pre>";
?>


Output:

gfg.php
index.html
neveropen.php

exec() Function

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.

Syntax:

string exec( $command, $output, $return_var )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $command: This parameter is used to hold the command which will be executed.
  • $output: This parameter is used to specify the array which will be filled with every line of output from the command.
  • $return_var: The $return_var parameter is present along with the output argument, then it returns the status of the executed command will be written to this variable.

Return Value: This function returns the executed command, be sure to set and use the output parameter.

Example:




<?php
// (on a system with the "iamexecfunction" executable in the path)
echo exec('iamexecfunction');
?>


Output:

neveropen.php

References:

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments