PHP Exec

PHP Exec

Last Updated on Mar 22, 2023


Did you know we can run system commands through PHP?

Well not only it’s possible but also it’s very easy.

You can easily run your commands with exec function


exec(command,output,result code)

command: the command you want to run. Like mkdir test

output: if there is any output for your command it will be saved on this variable

resultCode: the code that will be returned from running your command.

So let’s see a couple of examples example

Let’s create a directory called test

exec('mkdir test', $output, $resultCode);
// output is an empty array because there is no returned value
// resultCode is 0

Let’s get the user.

exec('whoami', $output, $retval);
// resultCode is 0
// output is:
// Array ( [0] => root )

see how easy it is?

The possibilities are endless!


Now you know about exec in PHP.

I recommend you to open a PHP files and try to run different system commands with PHP.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • run system commands
  • exec function

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts