Geek Brown Bag #69: Using PHP from the command line for fun and Profit!
Devo: Choosing your masterMatthew 6:24 tells us we cannot serve two masters, and gives us the example of serving God and Money, but there are tons of other masters we serve regularly. I'm going to take this a bit further and say you must serve a master. We are not built for spiritual independence, we have a need for God and our spiritual nature will not let us go without some sort of dependence.
A good way to determine if you are serving another master is to ask yourself how your thoughts, words, and actions are glorifying God. If they aren't, odds are you are serving someone or something else.
PHP from the Windows command line, for Fun and Profit!
PHP is not just limited to the browser! You can actually write command line scripts and launch them with PHP!- Open a command shell and type c:\path\to\php.exe c:\path\to\script.php
- Using shortcut commands
- add PHP to your system path
- open a command shell
- navigate to the directory of your script
- type php script.php (where script.php is the name of your php script)
- Create a batch file that does step 1 for you and double-click it!
@echo offWe'll start with step 2's method since it makes it easier to debug scripts. When you double-click a batch file, it just goes away once the script is done executing, and if it errors out, so it is hard to get started and comfortable with command line scripting.
c:\path\to\php.exe c:\path\to\script.php
Let's make a typical Hello World! script.
- Make a new file with your favorite text editor called helloworld.php
- put the following code in it:
echo("Hello World!");
?>
- now go to the command line and type php helloworld.php
Let's make an interactive script.
Now, what if you wanted it to say hi to you personally? Let's have it ask your name.
Make a new file with your favorite text editor called hello.php
put the following code in it:
hello.php
<?php
echo("Hey, you... I totally remember your name... but how do you like it spelled? ");
$name = trim(fgets(STDIN));
echo("\n\nAwesome " . $name . ", I'll be sure to remember that!\n");
?>
now go to the command line and type php hello.php
Sweet!
But how else can we send data to the PHP scripts? What about all those crazy command line parameters that other tools use?
Let's modify hello.php to let us tell it our name:
Open up hello.php again in your favorite text editor.
Add the following lines to check for command line arguments:
hello.php
<?php
if(isset($argv[1])) {
echo("Hey " . $argv[1] . ", how's it been?\n");
} else {
echo("Hey, you... I totally remember your name... but how do you like it spelled? ");
$name =trim(gets(STDIN));
echo("\n\nAwesome " . $name . ", I'll be sure to remember that!\n");
}
?>
now go to the command line and type php hello.php
Let's make a double-clickable script!
Create a new file in your favorite text editor called hello.bat
put the following code in it:
hello.bat
@echo off
c:\path\to\php.exe c:\path\to\hello.php
pause
Now find that file and double-click it!
Now, let's take some drag and drop-able command line arguments!
Open hello.bat in your favorite text editor
Add "%*" to the end of the second line:
hello.bat
@echo off
c:\path\to\php.exe c:\path\to\hello.php %*
pause
Now drag a file on top of hello.bat and let go!

