Section 3.1. Running PHP Scripts


3.1. Running PHP Scripts

You can execute your scripts in one of two ways: through a web server where the output is sent to a web browser, or through the command-line interface (CLI) where the output is sent to standard output. Of the two, the former is more popular, but the latter is steadily growing in popularity.

The primary difference between outputting text to the command line and to a web browser is the format of new linesthrough the CLI, you need to use \n for a new line, whereas for web browsers, you need to use the HTML line break, <br />. If you want to take a script designed for CLI and make it work through the Web, swap \n for <br />, and vice versa for converting web scripts to command line scripts.

If everything is configured properly, running scripts through your web server is as simple as putting the PHP script into your web server's public directory, then navigating to the appropriate URL with your browser. Running scripts through the command line is done using the CLI interpreter, which, if you are using Windows, is php.exe in the directory of your PHP installation. That is, if you have installed PHP into c:\php, the CLI program will be c:\php\php.exe. If you are using Unix, the availability of CLI PHP is down to how you installed PHPmake sure and issue the command make install-cli after the rest of the configure and make install process in order to install it.

The technical term for the command-line interpreter version of PHP is the CLI SAPI. SAPI stands for Server Application Programming Interface, and this standard interface allows PHP to work on multiple web servers, or, in the CLI SAPI's case, the command line.


If you are unsure whether PHP is set up correctly, run the following script:

     <?php             phpinfo( );     ?>

That calls the function phpinfo( ), which outputs information on your PHP configurationhow it was configured, what server it is running on, what modules are available, and more. It is handy to keep around when you are developing, as it will answer most questions you have about configuration.

Once you have PHP working, you can try running some more complex scripts. For example:

     <?php             $name = "Bob";             $age = 27;             $double_age = $age + $age;             echo "Hello, $name!\n";             echo "You are $age\n";             echo "In $age years time you will be $double_age\n";     ?>

To run that through your local web server, save the file as first.php and place it in your public HTML folder. For Windows this is usually c:\inetpub\wwwroot, and for Unix this is usually /var/www/html, but the location of the Unix public HTML folder does vary greatly. Once the file is there, load it through your web browser using the URL http://localhost/first.php.

If you are running your scripts through the command line, you need to find the location of your PHP executable. On Unix, you can usually just run php and it will work, e.g., php first.php. On Windows, go to Start, Run, then enter cmd and press Return. Then type cd \php followed by Return, then php c:\location\of\your\script\first.php.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net