4 ms·
zuck-mbpl:~ zuck$ php j play some red hot chili peppers PHP as a shell scripting language. Old habits die hard, huh?
by jameshart 10y ago
zuck-mbpl:~ zuck$ php j play some red hot chili peppers
PHP as a shell scripting language. Old habits die hard, huh?
- Klathmon 10y agoIt's not a bad scripting language in that case. Quick startup, a good amount of CLI-specific APIs, available on many servers already, and no compiler or huge amount of tools needed. It's a bit verbose for little scripts that should be in something like bash, but it's a great alternative to something like python if you are more familiar with it.
- mfonda 10y agoIn addition to this, if a lot of the libraries you're using are written in PHP, it makes a ton of sense that command line scripts using those libraries would also be written in PHP.
- gfosco 10y agoA decade ago, I used it like this for years at work. PHP is/was an awesome CLI script language. I always felt this was one of PHPs stronger use-cases, but lesser known and not pushed anywhere. Node has taken this spot, for me, but I'll never look down on PHP... It was very good to me.
- tomschlick 10y agoWith projects like composer/symfony/laravel & the plethora of really good PHP packages out there, it is easy to write a good CLI with PHP these days.
- cutler 10y agoSimple text parsing in PHP7 is also faster than in Python, Ruby, Perl and NodeJS. I recently wrote some 1-liners to parse a 19Mb log file with a regex on my 2010 quad-core Mac. Python/Ruby: 2.2 secs. Perl: 1.4 secs. NodeJS: 0.9 secs. PHP7: 0.4 secs.
- iso-8859-1 10y agoCan you show your code, at least?
- cutler 10y agoSure: Ruby IO.foreach('logs1.txt') {|x| puts x if /\b\w{15}\b/.match? x } Python3 import re with open('logs1.txt', 'rt') as fh: for line in fh: if re.search(r'\b\w{15}\b', line): print(line, end=' ') Perl open my $fh, '<', 'logs1.txt'; while (<$fh>) { chomp; say if $_ =~ /\b\w{15}\b/; } close $fh; NodeJS var fs = require('fs'), byline = require('byline'); var stream = byline(fs.createReadStream('logs1.txt', {encoding: 'utf8'})); stream.on('data', function(line) { if (line.match(/\b\w{15}\b/)) console.log(line); }); PHP $fh = fopen('logs1.txt', 'r'); while (($line = fgets($fh)) !== false) { if (preg_match('/\b\w{15}\b/', $line)) echo $line; } fclose($fh);