This lesson gives you a fundamental understanding of PHP files, its configuration and How PHP works.
The PHP File
- PHP file will have a .php or .php3 extension; you also have the option of changing the default file extension using the web server's configuration file.
- A .php file should have all the PHP code within <?php (opening tag) and ?> (closing tag), then It will be considered as PHP language code and parsed accordingly. It is also useful when you want to write PHP code with HTML.
- PHP can be embedded inside HTML but in a slightly different fashion. In between the HTML tags where ever you want to make use of PHP code, you will have to use <?php /*PHP code*/ ?> and PHP will parse this.
- All PHP statements must end with a ; (semicolon) which is also called statement terminator. It is essential because semicolon tells the PHP interpreter to read this line as a single statement, and without this, you will see error messages while interpreting the code.
The php.ini File
php.ini is a plain text file that configures PHP settings. PHP interpreter reads the php.ini file to determine what settings to use. We will refer to this file from time to time in the course, but now, it is enough that you are aware of its existence.
How PHP Works
- When a user navigates in her browser to a page that ends with a .php extension, the browser sends an HTTP request to the web server, for example, you are typing the URL of the file index.php and hit enter, the browser will send the request to server and server will start finding this file on its file system. If the server finds the file, the server will send this file to PHP interpreter. Otherwise, the server will generate ERROR 404, which is also known as file not found.
- The web server only sends those files to an interpreter, whose file extension is .php, all other files such as .html, .htm and others are not sent to PHP interpreter even if they contain PHP codes inside them.
- Once the file is sent to the PHP interpreter, it starts finding all of the opening and closing PHP tags and then process the PHP code inside these tags.
- PHP interpreter also checks that whether there is a database connection or not, if it finds database connection then it will send or retrieve data from the database.
- PHP scripts are interpreted on the web server, and outcome (HTML) is sent back to the client machine.
As shown in the picture above, PHP interprets the page and primarily communicates with the file system, database, and email server, and then delivers a web page to the web server to return to the browser.