PHPWomen Site Home » Programming » Best Practices » PHP CLI Tips And Tricks
PHP CLI Tips And Tricks [message #2069] Sat, 28 June 2008 14:22 Go to next message
gerard  is currently offline gerard
Messages: 1
Registered: June 2008
Shiny and New

It's not called the Command Line Interface for nothing. I programmed in PHP for a long time without even looking at the PHP CLI. When I finally did, I started appreciating all of its usefulness and features. Now I'd say I use it almost every day.

I'll show you three quick tips that can boost your PHP development productivity. Let's dive right in.

`php -r`

Did you know you can execute a line of PHP with the -r option? Use it like this:

% php -r 'echo "Hello world!\n";'
Hello world!

Note that everything inside the single apostrophes must be valid PHP syntax, so mind your double quotes and semi-colons. Now, this has a number of uses. For example, how about some quick math?

% php -r 'echo sqrt(150)."\n";'
12.247448713916

Or finding out what week of the year we're in?

%$ php -r 'echo date("W") . "\n";'
26

Or finding out what day of the week Independence Day in the U.S. is on next year?

% php -r 'echo date("l", strtotime("2009-07-04")) . "\n";'
Saturday

Or generating a random number for the office raffle?

% php -r 'echo rand(0, 100) . "\n";'
33

Sometimes, I just need a bunch of random letters and numbers. I find a random md5 hash usually does the trick.

% php -r 'echo md5(uniqid(rand(), true)) . "\n";'
faa5f6cd713bcfb64aa0b297d62c5345

You get the idea.

PHP Interactive Shell

I don’t know if it’s really a shell but it gets the job done. Have you ever wanted to test a small snippet of PHP code? You might have a small test script somewhere under a web server root you can quickly access. It’s a pain. With interactive PHP, you can test these right from the command line!

% php -a
Interactive mode enabled
 
<?php
echo "hello world!\n";
hello world!
exit;
%

This is great for figuring out little problems or little snippets of code while developing. For example, I for one can never remember where the $haystack and where the $needle go in assorted string functions.

% php -a
Interactive mode enabled

<?php
// does the $needle or $haystack come first?
echo strpos("lo", "hello");
echo strpos("hello", "lo");
3
// so the $haystack comes first

Or how about figuring out that regular expression you need?

% php -a
Interactive mode enabled

<?php
// get rid of all characters but "abc"
$regex = "/^[abc]/";
echo preg_replace($regex, "", "abcdef");
bcdef

// oops, that gets rid of [abc] occurring at the line beginning
// let's try again
$regex = "/[^abc]/";
echo preg_replace($regex, "", "abcdef");
abc

// that's what I wanted

PHP Command Interpreter

This isn't really a command line tip, but you might it useful nonetheless, if you are on Unix. You know you can execute a PHP script by running `php myscript.php`. But did you know you don't even need the `php` command?

When Unix runs a script, it looks for a command interpreter to interpret the script at the very top of the file. The command interpreter line begins with the shebang string (#!). You probably have done this with shell scripts, but you can do it with PHP scripts as well. So, if you have a PHP script named myscript.php that looks like this:

#!/usr/bin/php
<?php

echo "Hello world again!\n";
?>

You can run this script by running the file name.

% ./myscript.php
Hello world again!

(Make sure the file is executable.) So if you have an awesome PHP script that does some task for you, or a PHP script that deploys your site, you can put the script in your $PATH and simply call it like any other Unix command.

That's about it. There are a few other PHP CLI features you may find useful. `php -l` will run a syntax check on PHP code. `php -i` will give you output similar to the phpinfo() function.

Are there any other interesting ways you've used the PHP CLI?


10100101000010101000111010111
Re: PHP CLI Tips And Tricks [message #2474 is a reply to message #2069 ] Sat, 13 December 2008 08:55 Go to previous messageGo to next message
otherbird  is currently offline otherbird
Messages: 1
Registered: December 2008
Shiny and New
Quote:


Are there any other interesting ways you've used the PHP CLI?


Sure thing.

php -m


gives the loaded module list. Saves tracking down the effective php.ini. In PHP 5.3:

php --ini


will track down the effective php.ini for you, which is a total boon if you happen to have PHP installed in more than one place. Also there are the useful reflection methods (not sure exactly when these were implemented):

 --rf <name>      Show information about function <name>.
 --rc <name>      Show information about class <name>.
 --re <name>      Show information about extension <name>.
 --ri <name>      Show configuration for extension <name>.


- when you're working offline, these can practically stand in for the manual!
Re: PHP CLI Tips And Tricks [message #2702 is a reply to message #2474 ] Mon, 15 June 2009 13:00 Go to previous message
mvmaasakkers  is currently offline mvmaasakkers
Messages: 7
Registered: June 2009
Location: Amsterdam, The Netherland...
Shiny and New

Another one:
php -q filename.php


Executes the script without the version on top, handy when you're doing cronjobs which only outputs when something goes wrong and get automatically mailed to you...


"Beware of programmers who carry screwdrivers."
- Leonard Brandwein
Previous Topic:7 Best Practices
Next Topic:Is this a good MVC article?
Goto Forum:
  


Current Time: Fri Sep 3 19:54:26 EDT 2010

Total time taken to generate the page: 0.00896 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.0.
Copyright ©2001-2006 FUD Forum Bulletin Board Software