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

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

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

    3nvisi0n The R3v0lu710n Super-Mod

    Definition: CLI - Command Line Interface
    Command prompt - the (normally) black box where you type commands to your computer in
    shell - same as command prompt
    Warning: This tutorial assumes the ability to use and operate a computer and experience working within a command line environment. If you do not know how to open a shell(prompt) this probably is not for you.

    Hey guys,

    There are a lot of people out there who know PHP or are at least familiar with it at a functional level. 98% of what people know about PHP and see for it deals with web applications, stuff running within a web server(such as Apache.) This meets the needs of most people BUT PHP is a much more powerful language to be limited to just that one medium. For myself PHP has become my goto language for all menial tasks, from file parsing to my shell code replacement, to bots, to just for fun applications. PHP if understood can do it all. That is the task of this tutorial to introduce you to the idea of running PHP in a medium that is not a web server.

    [Installing PHP]

    Linux:
    * Many Distros have the option to have PHP installed at run time, to check if you already have it installed, open a shell and type "php -version" If you get an output that is not an error then you have it installed, check the version and see to it that it is up to date(some of this tutorial will deal with PHP 5.3+ functions.)
    * If not installed follow the guide located at http://php.net/manual/en/install.unix.php

    Windows:
    * Download the PHP Zip file from http://windows.php.net/download/
    * I use the non-thread safe version but it does not matter for this tutorial
    * http://windows.php.net/downloads/releases/php-5.3.6-nts-Win32-VC9-x86.zip
    * Once downloaded extract the contents of the ZIP to any location you desire, I will be using C:\Program Files (x86)\PHP\
    path.jpg
    * You'll notice in the directory where you extracted the zip two 3 .exe files, these files are the files we will be calling from the command prompt to run php code. All of them are essentially the same just output differently (I assume you already know how to run programs from the command prompt.)
    php.exe - This is the main one we will be using, is displays raw output to the prompt
    php-cgi.exe - This is the one apache for example could use, it displays the output with some minor HTML formatting and includes headers to the browser.
    php-win.exe - Displays nothing(not even a prompt) just runs in the background whatever code you give it.
    * With PHP extracted you are almost done with installing, now for ease of use we will add the directory you extracted PHP to your Path environment variable. This is so you can be in any directory with the prompt and when you type "php.exe" or "php" it will search your php directory for the executable this way your code can be stored and run from anywhere on your computer not just where the exe is.
    * To do this first thing is to open up your System Properties dialog box.
    XP: Right Click on My Computer and click Properties
    Vista/7: Left Click Start Orb, Right Click Computer, Click Properties, the on the left 'Advanced System Settings'
    * Move in this dialog box to the 'Advanced' tab near the bottom is a button labeled 'Environment Variables click that
    * In the new box under 'System Variables' look for one called Path, double click that.
    * To the very end of the value labeled Variable value: add a semi-colon ';' followed by the path you extracted PHP to
    ex. ";C:\Program Files (x86)\PHP"
    extract.jpg
    * Now you can call on PHP from the command line ^_^

    Now lets make sure its working, close out any shell you already have open(to activate the variable you just changed it needs to be opened again)

    Go to your desktop, and Right Click->New->Text Document name it test.php(make sure you are not hiding extensions, if you don't know how to check this...Google)
    Open test.php and put the following inside

    <?php for($i=1;$i<=10;$i++) echo "$i\tHello World\n"; ?>

    Now launch a prompt on your desktop,(cd to your desktop if necessary) and type "php test.php" if everything has been done correctly test.php should run and output

    C:\Users\3nvisi0n\Desktop>php test.php
    1 Hello World
    2 Hello World
    3 Hello World
    4 Hello World
    5 Hello World
    6 Hello World
    7 Hello World
    8 Hello World
    9 Hello World
    10 Hello World

    Just for your own reference \t is the tab character and \n is the eol character(end of line), without \n all output would come on the same line a the prompt(no using <br> since this is not a web browser)

    In my next tutorial I will get into some actual coding within the prompt and things you can done.
    1 person likes this.
Thread Status:
This thread is more than 180 days old.

Share This Page