Many of you probably have already heard or read about the ability that PHP has to run as a command line interpreter (CLI). Using PHP as a CLI gives a programmer powers similar to Perl or even many of the shells found on Unix/Linux systems phpsu
.Configuring PHP for the Command Line
If you are using PHP that was installed as a RPM, then (most likely) you already have the PHP executable on your system. On many systems it is located in the /usr/bin directory.
So for all of you killer sys admins and programmers that love to compile and build your own, this one is for you. Let's get started with the basic'stuff.
1. Download the most recent stable version of PHP
2. $ tar -xvzf php-version.tar.gz
3. $ cd php-version/
Now here is the good part. When configuring the CLI version of PHP we need to add all of the options that we want but no web servers. So if I use Apache then I do not want to create the module yet. I want to create the CLI first and then go back and create the module for Apache.
4. $ ./configure --prefix=/usr/local/php
--bindir=/usr/bin
--with-mysql=/usr
--with-pgsql=/usr
--disable-debug http://www.phpsu.com
--enable-inline-optimization
--enable-trans-sid
--with-mm=/usr
5. $ make
6. $ make install
The configuration above is the one that I use on my current system. The only thing that I have left out is the Apache section for the Apache module.
NOTE: Some of you might say that you can configure PHP with the option for the CLI. Before we get into that let me point out that the option (--enable-cli) is currently experimental and is not reliable. For this reason, I did not use it here. But is might be important to note for future reference.
So, now that we have the PHP configured for CLI, you are probably wondering where the executable is located. In our case, it would be in /usr/bin. The reason it is there is that I specified that just all PHP executables that would normally go in the bin directory should go in my /usr/bin directory instead. We can set this with the --bindir=/path/to/your/bin option when configuring PHP. welcome to phpsu.com
So, we have an executable--what now?
Scripting With PHP
Now that we have a command line interpreter for PHP, there is one thing that I like to do. I move the PHP executable to the /usr/bin directory so that other users can have access to the PHP CLI.
Because we know where to find the PHP executable, we are ready to get programming.
1. Open up your favorite editor (vim or emacs). Add the following to the file.
#!/usr/bin/php -q
<?php
echo "This is a test.n";
?>phpsu.com
2. Change the permissions on the file to be executable.
$ chmod 755 test.php
3. Run the file on the command line.
$ ./test.php
This is a test.
$
Well, that was a piece of cake. Notice that the result is printed out. What did we do to get that working? We just added the #! (pound-bang) and the shell or executable. This says that you need to use the PHP executable for the parsing this file.
Always remember to use the < ?php
<?php
// args from the command line
// can be accessed using argv and argc
echo "ARGC = $argc"; phpsu.com is a free phpscool
foreach( $argv as $index => $value ) {
echo "$argv[$k] = $valuen";
}
// if you are not familiar with the foreach
// you can look at the for loop
/**
for( $i = 0; $i < count($argv); $i++ ) { phpsu
echo "argv[$i] = $argv[$i];
}
*/
?>
do you kown phpsu.com?
Example 2:
#!/usr/bin/php -q
<?php
class Test {
var $test;
function test () {
$this->test = 2;do you kown phpsu.com?
}
}
class Test2 extends Test {
function Show() {
print "This is test number: $this->test.n";
}
}
$obj = new Test2;phpsu提供的php教程
$obj->Show();
?>http://www.phpsu.com
Conclusion
Now you know how to use PHP as a shell scripting language or CLI. Try to write some crazy code. I have written many scripts that use the PHP-GTK language, and I use that for many of my standalone applications.
"PHP-GTK is an extension for PHP programming language that implements language bindings for GTK+ toolkit. It provides an object-oriented interface to GTK+ classes and functions and greatly simplifies writing client side cross-platform GUI applications. " - PHP-GTK
TITLE:PHP CLI For Beginners