Приглашаем посетить
Черный Саша (cherny-sasha.lit-info.ru)

PHP Programs

Table of Contents
Previous Next

PHP Programs

PHP programs are stored in standard text files, which you can create using any text editor (note that Macintosh text editors sometimes add line break characters which PHP has trouble understanding, so you should look for an option in such editors to save files with ‘UNIX-style linebreaks’). Normally, you will need to save the file with the file extension .php for it to be executed by a web server, although the extension can usually be configured to anything you like. Here is an example from Apache's httpd.conf file, where we have created a .prophp4 extension:

    AddType application/x-httpd-php .prophp4

Alternatively, if you are using PHP to write console programs, the file extension isn't important.

File Basics

PHP programs are executed in one of two ways: by a web server, or as a console program. PHP programs can be made available via a web server which has been configured to support PHP by adding them to the web server's directory structure, where you would normally locate HTML files. They are accessed from a web browser in exactly the same way as static web pages. When a browser requests a page ending in the .php extension the web server runs the program through the PHP engine.

When the PHP engine begins to execute the script, its default behavior is to output the contents of the file, unaltered. This output will either go to the browser that requested the page, or the console when executing as a script.

You can simply take an ordinary HTML page, rename it so it has a .php extension, and PHP will process it, but perform no work on it at all.

In order to include PHP instructions in the file, we need to ‘escape’ from this standard output mode, into PHP. This is done by enclosing PHP instructions in delimiters.

SGML processing instruction:

    <?
    ...
    ?>

XML processing instruction:

    <?php
    ...
    ?>

HTML editor-friendly script-style:

    <script language="php">
        ...
    </script>

ASP style, for editors that understand ASP tags but not PHP tags:

    <%
    ...
    %>

We'll be using the XML style throughout this book, but there is no functional difference between any of these sets of delimiters.

One code style which you may come across, although we won't be using it much because it can cause confusion, is the following:

    <?= ... ?>
    <%= ... %>

These abbreviated tags execute the enclosed single PHP expression, and replace the entire tag with the result. The following, though debatable practice, is an example:

    two plus two is <?= 2 + 2 ?>

The output of this would be:

    two plus two is 4

Most of the time, instead of using this style of delimiter to output values from PHP code, we'll use the echo command, as follows:

    two plus two is <?php echo(2 + 2); ?>

The echo command is one of the most common ways to add text to the output stream from within PHP delimiters. Most of the operations we will perform within PHP code blocks don't directly lead to any output at all, and when we want to add text which we have generated inside our program, we'll use echo to do it. We can use echo to print out text, numbers, or HTML markup. Really, anything that would normally be included in a web page. The crucial thing is that PHP allows us to perform all kinds of operations to determine exactly what output we should produce.


Table of Contents
Previous Next