PHP is no longer just a scripting language for web automation. It has grown very rapidly and now holds the position of the 3rd most popular programming language worldwide. PHP is now being widely used for CLI programs and even GUIs 😀
While working on a daemon script at Leevio, I was executing a shell script using the shell_exec() function. Rana vai pointed me to the system() function which is quite identical to it’s C equivalent. I was really glad to have found it at last. I was looking for a function that has interactive output like Python’s os.system() call. It was in PHP from PHP4 and I never looked into it! 🙁 Shame on me!
The system() function takes a command, executes it, prints out all the outputs of the command and returns the last line of the output. We can additionally pass a variable as the optional second parameter that will hold the entire return value.
1 2 3 4 |
<?php $last_line = system('ls', $retval); echo "{$last_line} \n {$retval} \n"; ?> |
The “ls” command lists all the contents of a directory. The above php script is supposed to print out all the contents of the current working directory aka CWD 😀