Sometimes you may find yourself in a situation where you want to run more than just PHP code. Perhaps PHP does not have a built-in function for what you want to do or you want to run some custom commands, such as executing an external program. There are multiple ways to achieve this, and we will learn how to do it in two different ways.
The usual way: exec() and shell_exec()
These two functions are likely the ones that come to your mind first when thinking about a way to execute a terminal command. Both of there are basic PHP functions, and they are very similar to each other. The biggest difference between these functions is that shell_exec() returns all output produced by the command while exec() only returns the last line of output by default.
These functions are simple to use: call the function and pass the command you want to run as the first argument, for example: $directoryListing = shell_exec(”ls”). This will run the ls command that lists all files and directories in the current directory. The list will be saved in the $directoryListing variable as a string. You could also use exec(”ls”) to get the last line of output.
These commands are usually enough if you want to execute simple commands using PHP, but sometimes you may need more advanced alternatives. There are a couple different ways to run terminal commands, and one popular way is to use the process component from the Symfony framework.
The advanced way: Symfony Process Component
Being a more advanced way to run terminal commands, this is also a litte more difficult to set up. We won’t cover setting up a Composer project here, but the easiest way to install the component is to require it using Composer. You can do so with composer require symfony/process. If you are not familiar with Composer, you can also download the process component from GitHub. You can read more about this on the official Symfony documentation page.
When you have set up the component, first you have to create a new instance of the process object. As the only parameter to the constructor, you can pass the command you want to run. An example looks like this: $process = new Process(”ls”). This will only create an instance of the process, but the command will not be executed yet. This is because you can specify some extra functionality before running the command. You can for example disable all output or set a timeout for the process.
When you are ready to run the comand, you can do so by calling $process->run() or $process→start(). Both of these will execute the command, but run() waits for the command to finish executing while start() runs the command asynchronously. This allows you to run multiple commands without slowing down your application.
As you can see, the process component of Symfony is a more flexible and powerful tool. It even allows you to return data in real-time and return incremental output from a command if you want to retrieve the output of a process at a later time.
No comments yet (leave a comment)