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.
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.
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.
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.
require: This will do the same as include, but PHP will throw an error instead of a warning if the file is not found.
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.
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:
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.
The web server continues in HTML mode until it encounters a PHP opening tag (<?php).
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.
The web server continues in PHP mode until it encounters a PHP closing tag (?>).
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.