PHP CLI Tips and Tricks
PHPWomen Contest Winner
ByPHPWomen.org recently held an article-writing contest on their Best Practices Forum. Authors of the two winning submissions each received copies of Zend Studio for Eclipse, a 1-year subscription to Linux Pro Magazine (which is called Linux Magazine outside North America), and the opportunity to feature their articles on the magazine websites. Congratulations goes to Gerard Sychay for his winning submission!
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.
In this article, 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. 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. falls 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 whether 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 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 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 might find useful. For example, php -l will run a syntax check on PHP code, and php -i will give you output similar to the phpinfo() function.
Are there any other interesting ways you've used the PHP CLI?
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.