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.
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.
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!
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:
- 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!
script.bat:
@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:
helloworld.php
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!
No comments:
Post a Comment