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.

You can include one file from another PHP file using any one of these four functions:

  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. Just think carefully what your scenario is, and then decide.

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.