Getting PHP’s version from the command-line

As you may already know, obtaining the version number of PHP is a simple php --version away. This is great if you just want to see the version number, but what if you wanted just the version number? First option would be to use write out some shell script to snag just the version number:

php --version | head -n 1 | awk '{print $2}'

That line will pipe the blog of PHP version information to head which we tell to only give us the first line (the one with the version number in it) and then pipes it to awk to print out the second word (which is the version number). It gets the job done but if you wanted, you could accomplish the same by leveraging PHP’s CLI interface:

php -r 'echo phpversion();'

Definitely not as much going on in the PHP-only solution but it gets it done. I did do some rudimentary speed tests and both solutions performed in about the same amount of time.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.