How to execute shell command from PHP?

| | 1 min read

I had a requirement where I had to execute shell command from PHP. The requirement was that I had to find details of a domain using commands like whois and then store this information into files in a folder. The only input parameter for the process was the name of the domain which was passed on from the Drupal site. The PHP function (shell_exec) was designed to do things like this.

Example code:

// Site url
$site_url =

// Sample code to test if the code works fine on your local machine.
// Instead of the code given below you can add code to create directory
// for the user depending upon the condition.
// Keep in mind that each call will open a new shell instances.
$outping = shell_exec("ping -c2 -w2 $site_url") ;

// You can place the outping inside pre tag to see the output clearly.
echo "$outping";