External Commands / Programs

Used to execute external programs (e.g. system commands).

 

Take at least one parameter: the command /program to be executed.

 

exec() runs the command/program and sends back the last line output from that program as its return value.

 

exec( "command", $output, $return ) ;

 

Optional parameters:

  • 2nd parameter will contain the output of the command as an array with one line per element.
  • 3rd parameter contains the return value of the command.

 

<?php
	exec("ls -a", $output, $return);

	echo "The command ls - a returned $return, and output:<br>";

	echo "<pre>";

	print_r( $output );

	echo "</pre>";
?>

 

passthru() runs the command/program and displays all of the raw output directly to the screen.

 

passthru( "command", $return ) ;

 

Optional parameters:

  • 2nd parameter contains the return value of the command.

 

<?php
	echo "The command ls - a in passthru() displays: <br>";

	passthru("ls -a") ;
?>

 

 

system() runs the command/program and displays the raw output directly to the screen.

 

system( "command", $return ) ;

 

<?php
	echo "The command ls - a in system() displays: <br>";

	system("ls -a") ;
?>

Leave a Reply