Wednesday, October 28, 2009

The Execution Operator








The Execution Operator


Here's a cool onethe execution operator lets you run operating system commands and programs. All you've got to do is to enclose the command you want to run in backticks (`not the same as a single quote!). Here's an example that runs the date system command, captures the output, and displays it:



<?php
$output = `date`;
echo $output;
?>


Here's the kind of result you might see under Unix, using the bash shell:



-bash-2.05b$ php t.php
Thu Aug 12 10:53:28 PDT 2004


Because date is also a command in DOS, here's what you might see in a DOS window:



C:\php>php t.php
The current date is: Thu 08/12/2004
Enter the new date: (mm-dd-yy)


What if you want to pass an argument to the system command? Include the argument in the backticks too, as in the following, where we're executing the command dir c:\temp in Windows:



<?php
$output = `dir c:\\temp`;
echo $output;
?>


The result appears in Figure 2-4.


Figure 2-4. Executing system commands.

[View full size image]









    No comments:

    Post a Comment