Joomla!, Wordpress tips for building your site

Joomla!, Wordpress tips for building your site

Syndicate this site using RSS

How to use php with console (command line)

Published on| July 17th, 2009 | No Comment.

Perhaps I guess that you think that PHP language is very useful WEB language.
PHP usage is not only that.
PHP can also use from the command line.

Very famous CMS (Ex. Joomla! , WordPress etc,.) is often developed by PHP and MySQL.
I often use Joomla! and WordPress because of being developed by PHP (and open source).
So, when I have problem, I will clear problem very easy. Also it is a favor of open source.

Well, I will speak how to execute PHP with command line from now.
It is very simle.
If you want to execute PHP from command line, you will only input following characters.

> php test.php

Of course, first, you must add environment of PATH "php installation directory".
Next, you must create ‘test.php’ file.
following example of ‘test.php’ will only display characters of ‘Hello World’ on command line.

<?php
echo 'Hello World!';
?>

When you run ‘test.php’, following characters will display on command line.

> php test.php
Hello World!
>

Other , when you execute PHP from command line, you can specify the command parameters like a follows.

> php test.php --help

If you want to input the command parameters, you should rewrite ‘test.php’ for append functions of command parameters.
Following code is example of ‘test.php’.

<?php
if ($argc == 2 && in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
<?php echo $argv[0]; ?> --help
Display Help.
<?php
} else {
        echo 'Hello World!';
}
?>

$argc and $argv can be used with PHP as a variable reserved to the way just like C language.

$argc is count of parameters (if $argc equal 1, it means parameters is nothing.)

$argv are the parameters.

When you run ‘test.php’, following characters will display on command line.

> php test.php --help
test.php --help
 
Display help.

You will be able to write script program very easy with PHP. Specially you will feel very easy when you write script of WEB and HTML.
I think PHP works very well.

This post is very basic PHP usage. ( :D )


Add to your favorites(bookmarks): はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing

Trackback URL

After Admin approves this comment, it will be shown.


Comments

Leave a Reply





*