Back to Top

How to execute PHP code from the command line?

How to execute PHP code from the command line?

PHP supports functions which allow you to execute PHP code from the command line. You don’t need a browser to execute your PHP code. Today I am going to cover exec() function to execute external command using PHP.

exec(): Executes an external command but print nothing. It simply returns the last line of the command.

SYNTAX:

PARAMETER LIST

command – The command you want to execute
output – If output parameter is specified, the array will be filled with the output of the code.

Note that if the array already contains some elements before the exec () function call, the output of the command will be appended to the end of the array. If you do not want to combine with the previous contents of the array, you should call the function unset () with the name of the array as an argument before passing it as an argument to the exec().

return_var – If both the parameter return_var and output, when you exit this variable will contain the completion status of the external program.

Examples to use exec() fucntion

Let’s execute the basic command using the exec() function

The exec() function calls the program directly without any intermediate shell and the backtick execute the program thru a shell.

There is one more technique to use the pipeline command using 2 > &1. You can this in exec() command and it will output the shell execution error to the output of the variable.

Example

You can write like this:

Why you need the exec() in PHP?

exec() is a function and is mainly needed to execute any command using PHP. You can also execute the external command without outputting the result or response.If the output variable is set to the function, all the execution results are included into that variable in an array form. If you need to use it to run it in the background of the program, make sure that the output has been redirected to another file, otherwise, the php will wait for it to complete the implementation and then it will continue to the browser output.

Note: If your program starts with this function and want to leave it running in the background, you should make sure that the output of this program will be sent to a file or some other output stream or else PHP will hang until the execution of the program ends.

At the same time, I want to finish. I hope everything is clear and now you have to deal with this function. If you are using exec() function to execute PHP code, let me know in the comment section that how it is useful to you? Thank you for attention. Error-free execution of your code!

Share:
Share on Facebook1Share on LinkedIn1Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit1

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

Getting Started with Google Apps Script

Posted on 1 year ago

Bhumi

How To Repair MySQL Table

Posted on 5 years ago

Bhumi

How To view module Position in Joomla

Posted on 5 years ago

Bhumi

Static Methods and Properties in PHP

Posted on 5 years ago

Bhumi

Facade implementation in Laravel

Updated 3 weeks ago

Bhumi

MySQL coalesce() function

Updated 17 years ago

Bhumi

How to use JavaScript Promise API?

Updated 17 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi