Tuesday, November 18, 2008

GBB #69: Using PHP from the command line for fun and Profit!

Geek Brown Bag #69: Using PHP from the command line for fun and Profit!

Devo: Choosing your master

Matthew 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.

We may choose to serve God, money, pleasure, entertainment, philanthropy, culture, etc. As geeks it is easy to slip into serving and worshiping the geek culture that oftentimes ties us together and builds our friendships. What we should be doing is taking a step back and asking how much are we serving our God, and how much are we serving our culture.

God, money, pleasure, entertainment, culture, twitter, etc. are not bad things, but we've got to prevent ourselves from serving and worshiping anything or anyone other than God.

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.

Romans 6:1-11 describes the fact that we should not continue sinning just so "grace may increase" because we have died to sin. We are no longer slaves to sin, because we share in Christ's death and resurrection, and anyone who has died has been freed from sin.

The rest of Romans 6 describes that just because we are free from sin doesn't mean we are no longer slaves, just that we are now slaves to righteousness. Even though we are under God's grace, that doesn't mean we should sin, because when we accepted His grace, we offered ourselves to Him. We are slaves to who we obey--- whether to sin, which leads to death, or to obedience, which leads to righteousness.

Every day, we make the choice not to "be good" or "be bad", but to serve God or serve sin. (I recommend serving God, even if I'm not always the best at it.)

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!

Three primary ways to launch a PHP app:

  1. Open a command shell and type c:\path\to\php.exe c:\path\to\script.php
  2. Using shortcut commands
    1. add PHP to your system path
    2. open a command shell
    3. navigate to the directory of your script
    4. type php script.php (where script.php is the name of your php script)
  3. Create a batch file that does step 1 for you and double-click it!
script.bat:
@echo off
c:\path\to\php.exe c:\path\to\script.php
We'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.

Let's make a typical Hello World! script.

  1. Make a new file with your favorite text editor called helloworld.php
  2. put the following code in it:

helloworld.php
echo("Hello World!");
?>
  1. now go to the command line and type php helloworld.php
There you go! Your first PHP command line script!







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.








  1. Make a new file with your favorite text editor called hello.php


  2. 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");

?>




  1. 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:








  1. Open up hello.php again in your favorite text editor.


  2. 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");

}

?>




  1. now go to the command line and type php hello.php









Let's make a double-clickable script!








  1. Create a new file in your favorite text editor called hello.bat


  2. put the following code in it:







hello.bat



@echo off

c:\path\to\php.exe c:\path\to\hello.php

pause




  1. Now find that file and double-click it!








Now, let's take some drag and drop-able command line arguments!







  1. Open hello.bat in your favorite text editor


  2. Add "%*" to the end of the second line:







hello.bat



@echo off

c:\path\to\php.exe c:\path\to\hello.php %*

pause




  1. Now drag a file on top of hello.bat and let go!