Slide 1

Slide 1 text

#!/usr/bin/php PHP on the command line Tuesday, February 19, 13

Slide 2

Slide 2 text

./phpcl --overview • Basic PHP Commands • PHP Scripts • Arguments • Real World Example Tuesday, February 19, 13

Slide 3

Slide 3 text

Practical PHP On the command line Tuesday, February 19, 13

Slide 4

Slide 4 text

php -v → php -v PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies Tuesday, February 19, 13

Slide 5

Slide 5 text

php --ini → php --ini Configuration File (php.ini) Path: /etc Loaded Configuration File: /private/etc/php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) Tuesday, February 19, 13

Slide 6

Slide 6 text

php lint Tuesday, February 19, 13

Slide 7

Slide 7 text

php -l → php -l stdin.php No syntax errors detected in stdin.php Tuesday, February 19, 13

Slide 8

Slide 8 text

php -l → php -l stdin.php PHP Parse error: parse error in stdin.php on line 5 Errors parsing stdin.php Tuesday, February 19, 13

Slide 9

Slide 9 text

php -l • Automate during rollup, commits • Prevent silly mistakes, because, let’s face it, you aren’t doing TDD • Sometimes tools might modify output, even accidentally • Ensures code can run on the version of PHP installed Tuesday, February 19, 13

Slide 10

Slide 10 text

php -a PHP’s interactive shell Not quite a REPL, doesn’t really matter (Read-Evaluate-Print Loop) Tuesday, February 19, 13

Slide 11

Slide 11 text

php -a → php -a Interactive shell php > $a = range(1,3); php > var_dump($a); array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } Tuesday, February 19, 13

Slide 12

Slide 12 text

php -a php > class Foo { public $bar = 'baz'; } php > $foo = new Foo(); php > echo $foo->bar; baz Tuesday, February 19, 13

Slide 13

Slide 13 text

php -a php > function p($v){ php { echo md5('this_is_really_secure_right?'.$v); php { } php > p(134); bb33942de0b7f8f7fda76ef41ee55654 php > p('happy'); fbc327fa6e16a971130d72bd72e7e55f Tuesday, February 19, 13

Slide 14

Slide 14 text

php -a php > var_dump(array()===array()); bool(true) php > var_dump( ((array) new stdClass()) === array() ); bool(true) php > class Foo{function __toString(){return '';}} php > $foo = new Foo(); php > var_dump( ((string) $foo) === '' ); bool(true) Tuesday, February 19, 13

Slide 15

Slide 15 text

php -a • Great for resolving quick question • Winning arguments • Temporary scripts, functions Tuesday, February 19, 13

Slide 16

Slide 16 text

php -a • Has autocomplete php > new PDO PDO pdo_drivers PDOException PDORow PDOStatement Tuesday, February 19, 13

Slide 17

Slide 17 text

PHP Scripts Tuesday, February 19, 13

Slide 18

Slide 18 text

Running Scripts • Command Line • $ php ./script.php • Bash Bang • #!/usr/bin/php Tuesday, February 19, 13

Slide 19

Slide 19 text

Command Line • Useful for executing code that might also run from a web server • Doesn’t assume existing installation directory Tuesday, February 19, 13

Slide 20

Slide 20 text

Bash Bang • $ ./script.php • $ ./script • $ script • #!/usr/bin/env php > #!/usr/bin/php Tuesday, February 19, 13

Slide 21

Slide 21 text

getopt string(5) "hello" ["p"]=> string(4) "asdf" ["h"]=> bool(false) } Tuesday, February 19, 13

Slide 22

Slide 22 text

getopt • f: - Required value • v:: - Optional value • a - No value • $opts = getopt(‘f:v::a’); Tuesday, February 19, 13

Slide 23

Slide 23 text

getopt • getopt( $shortOptions, $longOptions ) Tuesday, February 19, 13

Slide 24

Slide 24 text

getopt long options • $longOptions = array( • ‘required:’, • ‘optional::’, • ‘option’ • ); • $ script --required 123 --optional=”value is here” --option Tuesday, February 19, 13

Slide 25

Slide 25 text

getopt • $opts = getopt(‘v’,array(‘verbose’)); • $ script -v • $ script --verbose Tuesday, February 19, 13

Slide 26

Slide 26 text

getopt • php.net/getopt • > 5.3 Works on windows, plus support for ::, =, and longopts • < 5.3 misses those critical features Tuesday, February 19, 13

Slide 27

Slide 27 text

time to see code Tuesday, February 19, 13