1.1. How PHP Works

The PHP software works with the Web server. The Web server is the software that delivers Web pages to the world. When you type a URL into your Web browser's address bar, you're sending a message to the Web server at that URL, asking it to send you an HTML file. The Web server responds by sending the requested file. Your browser reads the HTML file and displays the Web page. You also request a file from the Web server when you click a link in a Web page. In addition, the Web server processes a file when you click a Web page button that submits a form.

NOTE

How the Web server processes PHP files

When a browser is pointed to a regular HTML file with an .html or .htm extension, the Web server sends the file, as is, to the browser. The browser processes the file and displays the Web page described by the HTML tags in the file. When a browser is pointed to a PHP file (with a .php extension), the Web server looks for PHP sections in the file and processes them instead of just sending them as is to the browser. The Web server processes the PHP file as follows:

  1. The Web server starts scanning the file in HTML mode. It assumes the statements are HTML and sends them to the browser without any processing.

  2. The Web server continues in HTML mode until it encounters a PHP opening tag (<?php).

  3. When it encounters a PHP opening tag, the Web server switches to PHP mode. This is sometimes called escaping from HTML. The Web server then assumes that all statements are PHP statements and executes the PHP statements. If there is output, the server sends the output to the browser.

  4. The Web server continues in PHP mode until it encounters a PHP closing tag (?>).

  5. When the Web server encounters a PHP closing tag, it returns to HTML mode. It resumes scanning, and the cycle continues from Step 1.


When PHP is installed, the Web server is configured to expect certain file extensions to contain PHP language statements. Often the extension is .php or .phtml, but any extension can be used. (In this book, we assume that .php is the extension for PHP scripts.) When the Web server gets a request for a file with the designated extension, it sends the HTML statements as is, but PHP statements are processed by the PHP software before they're sent to the requester.

When PHP language statements are processed, only the output is sent by the Web server to the Web browser. The PHP language statements aren't included in the output sent to the browser, so the PHP code is secure and transparent to the user. For instance, in this simple PHP statement:

<?php echo "<p>Hello World</p>"; ?>

<?php is the PHP opening tag, and ?> is the closing tag. echo is a PHP instruction that tells PHP to output the upcoming text. The PHP software processes the PHP statement and outputs the following:

<p>Hello World</p>

which is a regular HTML statement. This HTML statement is delivered to the user's browser. The browser interprets the statement as HTML code and displays a Web page with one paragraph — Hello World. The PHP statement isn't delivered to the browser, so the user never sees any PHP statements. PHP and the Web server must work closely together.

PHP isn't integrated with all Web servers but does work with many of the popular Web servers. PHP is developed as a project of the Apache Software Foundation — thus, it works best with Apache. PHP also works with Microsoft IIS/PWS, iPlanet (formerly Netscape Enterprise Server), and others.

If you can select or influence the selection of the Web server used in your organization, select Apache. By itself, Apache is a good choice. It's free, open source, stable, and popular. It currently powers more than 60 percent of all Web sites, according to the Web server survey at www.netcraft.com. It runs on Windows, Linux, Mac OS, and most flavors of Unix.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.37.62