Change background image
  1. What's up? I see you're viewing as a Guest. How about registering, it only takes like 2 minutes. This will enable you to do more on our forum and stay updated.

PHP and the CLI - Why Bother?

Discussion in 'PC General' started by 3nvisi0n, Jun 19, 2011.

Thread Status:
This thread is more than 180 days old.
  1. 3nvisi0n

    3nvisi0n The R3v0lu710n Super-Mod

    Okay so in part one you got setup, now what was the point of that? What can you do with the command line,

    Well for one in my opinion it makes developments of applications possible. Instead of being limited to websites you can now create programs like you would in a language like C or Java but using all the greatness of PHP.

    One thing is you can now use PHPs command line options
    for example one cool one(although I never use it) is the interactive shell(*nix only at the moment)
    php -a
    You can read about it at the above link. Its cool but not practical at least for any of my needs since use a good editor...[shameless plug for phpDesigner 7]

    Beyond that there are some actual useful stuff that you can do from the command line if you are using this to replace your other shell scripting languages(as I do :D) but I won't bore you with the details, instead will

    I will only mention some important ones in my mind, thoses being -B -F -E
    -B will execute a small piece of code at the beginning of ur input
    -E is addition code to be run at the end
    and -F is a php file to be run with every input.
    Why bother, well lets say for example you often need to censor files to remove foul language from for example an XAT chat long from a chat like newhaxdotcom. So you get your log files lets say generated on an hourly basis. in the form log.HH.DD.txt where HH is the hour(00-24) and DD is the date(01-31) to do this by hand would be annoying even if you wrote a php file to auto open all the files following that format, and what if the file naming scheme changed then you'd have to edit your code, so it isn't very movable then. Its only good for you and no one else(no sharing :()

    The solution, use the command line to pipe in the files(google how to redirect input so you can use it in the CLI) you'd then be able to run it with something like an 'ls' command rather than needing to hardcode it all in.

    SO you would use something like ls | php -F censor.php
    MAybe you also wanted to count the number of failes it parsed also(don't know why but its just an example) you could use
    ls | php -B '$count=0;' -R '$count++;' -F censor.php -E 'echo "Total files:$count";'
    This makes your code portable so you can use it for a number of problems not just what you specifically coded it for the the -B and -E allow for end and start codes :)

    *This is just an 'example' things do not work exactly as I explained but its close enough for you to get the idea and do your own stuff on it.
Thread Status:
This thread is more than 180 days old.

Share This Page