PHP Files

In order to write PHP code, you have to start the file with <?php. You can also finish the file or PHP code with ?>. You can mix PHP code with other content, like HTML, CSS, or JavaScript, in your PHP file as soon as you enclose the PHP bits with the <?php and ?> tags.

Example of PHP Script

echo 'Hello World';

The PHP tags enclose only one statement - an echo statement. The echo statement is used frequently. The output is simply the text that is included between the single or double quotes.

Importing Files

The same code often needs to be called on multiple pages. This can be done by first placing the code inside a separate file and then including that file using the include statement. This statement takes all the text in the specified file and includes it in the script, as if the code had been copied to that location. Just like echo, include is a special language construct and not a function, so parentheses should not be used.

include 'myfile.php';

An include file can either be specified with a relative path, an absolute path, or without a path.

In addition to include, there are three other language constructs available for importing the content of one file into another: require, include_once, and require_once.

  1. include: This will try to find and include the specified file each time it is invoked. If the file is not found, PHP will throw a warning, but will continue with the execution.

  2. require: This will do the same as include, but PHP will throw an error instead of a warning if the file is not found.

  3. include_once: This function will do what include does, but it will include the file only the first time that it is invoked. Subsequent calls will be ignored.

  4. require_once: This works the same as require, but it will include the file only the first time that it is invoked. Subsequent calls will be ignored.

Each function has its own usage, so it is not right to say that one is better than the other. The include_once and require_once statements may be used instead of include and require in cases, where the same file might be imported more than once during a particular execution of a script.

How Web Server Processes PHP files

When a browser is pointed to a regular HTML file with an .html 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 or, more exactly, hands them to the PHP processor, instead of just sending them as is to the browser. The web server or PHP processor 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 hands the processing over to the PHP module. This is sometimes called escaping from HTML. The web server then assumes that all statements are PHP statements and uses the PHP module to execute the PHP statements. If there is output from PHP, 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 language statements are processed, only the output, or anything printed to the screen,is sent by the web server to the web browser. The PHP language statements, those that don’t produce any output to the screen, are not included in the output sent to the browser, so the PHP code is not normally seen by the user.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive