Run PHP Files From the Command Line

By  on  

I've been brushing up on my shell scripting lately. I just got a MacBook and never felt compelled to spend too much time with Cygwin. I'm learning quite a bit now but there are still some tasks that I'd need to accomplish sooner rather than later. I know how to accomplish the task using PHP so I've got that to my advantage. What's awesome is that I can quickly and easily run my PHP files from the command line.

The Shell Script

php doTask.php

I'm not sure whether I consider using PHP instead of a straight shell script as being resourceful or using PHP as a crutch. What are your thoughts?

Recent Features

Incredible Demos

Discussion

  1. I don’t consider it a crutch. There are many useful reasons to run PHP in command line. You may have already seen this, but IBM has a nice article about it here: http://www.ibm.com/developerworks/opensource/library/os-php-command/

  2. I’m also using PHP scripts from the command line, but instead of invoking scripts through php executable I prefer the ‘#!/usr/bin/php’ header at the top of the script and marking it as executable. It’s like a RAD tool for me and, I believe, for others too.

  3. PHP makes a decent command line language, but Perl is more suited to the task imho

  4. Since I run windows and haven’t the heart to setup cygwin+php, I made a little php executer which more or less works like a command line. It doesn’t use eval but I think it could still be better. I’d advise to put this in a production environment of course, just development.

  5. More often than not I work in the command line with vim. running php from the command line is better suited for perl, but I do use it from time to time with php scripts.

    Primarily scraper scripts where I want to ensure the code is returning the values I want. It’s quick and easy to get the dirt.

  6. Justin Noel

    I’m a telecom programmer. I process hundreds of millions of records daily – with PHP. Yes, I know – awk is faster, perl is faster…..

    Try interfacing with a database using Awk. To run a query, you have to use mysql -e and pass an SQL statement. Need to get data? Do the same with select into outfile. No thanks!

    Perl? Such ugly code. No thanks.

    Guess what, PHP does a great job and is fast enough to meet my needs. I literally have dozens of scripts that process different data sets each day.

    PHP CLI rocks!

  7. The good thing about being able to run PHP from the command line is you can use the framework/libraries that your website uses.

  8. I’ve come to agree: not a crutch. It works for a reason. PHP (and MooTools) FTW!

  9. davidck

    I use php for some cron jobs :)

  10. Are you following me? I just setup my work PC to run PHP for the command line. Do you have cygwin setup to run through your command line as well? I find it more and more useful.

  11. Patrick Langendoen

    I’m a PHP CLI fan as well.. In addition to the other posts I have the following use as well:
    I have quite a few scripts that will run continuously and when you press CTRL-C (SIGINT) you process this info and show some statistics for the retrieved data.
    Try that on a website, it would be nearly impossible to do this safely without a chance that this test will run for days or weeks. (without the chance to see the statistics after warts)
    If you combine the above with the use of ‘screen’, you get the change to do some testing over days and see the statistics of that after (for instance) a week.
    I use this for testing connection and if things are working properly.
    This could have been done with perl or bash as well but the same problem as stated earlier arises: talking with a sql database is tough.
    Aside from this I just like php more than other languages.

  12. I invoke the Salesforce API with CLI PHP to do various testing and administration tasks. I also use it to generate CSV from mysql. I like Perl, but it’s a matter of preference, not a crutch.

  13. @davidck: me too :D

  14. Pete

    It really depends on what you are trying to do. PHP, Perl, shell scripting, they all have a place, if I want to automate a bunch of system tasks then I use a shell script, if I want to do some db work then probably Perl. Its important to use a tool because it is the right tool, not because you don’t know how to use anything else.
    Just my opinion.

  15. my stupid school has disabled javascript. i cant even check my email anymore. i have been able to access cmd prompt and internet options is blocked as well as you can imagine. so i want to know the cmd to enable javascript for my internet explorer browser.

  16. @kyle Google it – there will be a registry entry somewhere that you can change with reg.exe (regedit is probably disabled in some way).

  17. Hi all,

    I was wondering is it possible to run a website using a command line? Something like:

    php -f http://intranet-website.com/icivf/graph.php

    Thanks

  18. Patrick Langendoen

    @Liao:

    The webserver should allready have parsed this php file, it would make no sense to run it through (a local) php again, because there will be no more php tags and commands left in it for your local php to parse.
    What are you trying to accomplish ?

  19. Liao Gangzheng

    I created a website that generates a new chart every time a user surf a web page. I dun want that. I want the codes to run via cli instead

  20. Patrick Langendoen

    @Liao Gangzheng: I guess what you’ve accomplished there is some sort of cronjob, triggered by a visitor requesting some php script on your webserver?

    You can schedule your php script using the crontab.
    If you like you could do so using your local webserver, but I would prefer a direct link to the filesystem (/path/to/file instead of http://server.fqdn/path/to/file)

  21. Noodle Schneider

    Hi there!

    I just installed cygwin but can’t make php command execute properly. I got the following error when executing php command only.

    PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) ‘msql.dll’
    in Unknown on line 0
    PHP Warning: PHP Startup: Unable to load dynamic library ‘D:\PHP\ext\msql.so’ –
    The specified module could not be found.
    in Unknown on line 0

    I already have PHP, Mysql and Apache installed on my machine and it’s all working properly. I have scripts that need to be executed that is why really need this.

    Could somebody help me on this? Please…

  22. nimmee

    I don’t consider it a crutch. There are many useful reasons to run PHP in command line. You may have already seen this, but IBM has a nice article about it here:

  23. Thanks for it. It helped me to run my php scripts without need of accessing them on browser.

  24. It is a crutch if you are using it for something that it is really bad at only because you know the language so much better.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!