All of the operators we have looked at so far are similar to those available in other programming and scripting languages. With the execution operator, however, we begin to experience the power of PHP as server side scripting environment. The execution operator allows us to execute a command on the operating system that hosts your web server and PHP module and then capture the output. |
You can do anything in an execution operator that you could do as if you were sitting at a terminal window on the computer (within the confines of the user account under which PHP is running). Given this fact, it should not escape your attention that there are potential security risks to this, so this PHPfeature should be used with care. |
The execution operator consists of enclosing the command to be executed in back quotes (`). The following example runs the UNIX/Linux uname and id commands to display information about the operating system and user account on which the web server and PHP module are running. |
(The least complicated of all three special operators is the execution operator, which is ` – a back tick. Back ticks are used very rarely in normal typing, so you might have trouble finding where yours is – it is almost certainly to the left of your 1 key on your keyboard.) |
Note: that these command will not work if you are running a Windows based server. |
<?php echo `Hello World!` . '<br>'; echo `id`; ?> |
This results in the following output in the browser window: |
Linux EbizLinuxHome 2.6.9-42.0.10.ELsmp #1 SMP Tue Feb 27 10:11:19 EST 2007 i686 i686 i386 GNU/Linux |
uid=48(apache) gid=48(apache) groups=48(apache) |
Sample PHP script: |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>PHP: Executing Server side Command </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY><H1>Executing Server Side Command </H1> <?php $dirToCreate="testDIR"; ?> <hr> <FIELDSET> <LEGEND><span style="background-color: #3399CC ; color: #F7F7F7;font-weight: bold"> Executing Server Side Commands </span></LEGEND> <pre> <?php echo "<br>Before Executing the <i>mkdir</i> commnad"; echo `ls -la`; echo "<hr width=75%>"; echo "<br>After Executing the <i>mkdir</i> commnad"; echo `mkdir $dirToCreate`; echo `ls -la`; echo "</b>"; ?> </pre> </FIELDSET> </BODY> </HTML> |
PHP script above executes some basic Linux commands and list the output in a HTML format. Type the above script and save it as serverCommand.php and uploaded it to a Linux based server to view the output: |
![]() |
Assignment |
1. Write a PHP script to implement all Arithmetic and Assignment operators, display the output along with variable value on the browser. 2. Write a PHP script to implement Unary increment and decrement operators. 3. Write a PHP script to print following series, using Unary increment operator |
10 9 8 . 0 |
4. Write PHP Script to implement following Unix commands: |
a. ls –a b. ls / -la c. mkdir hello d. dir / |