Chapter 4. The PHP Language
This chapter forms a complete introduction to the basics of PHP programming, covering variables, comments, conditional statements , loops, and more. If you have little experience with PHP, this is the best place to start. Otherwise, you may only need to dip into parts of this chapter to refresh your memory.
The Basics of PHP
By default, PHP operates with PHP mode turned off, which means that PHP will consider the content to be plain text (i.e., not PHP code) unless PHP mode has been enabled. This method of parsing means that the PHP elements of a script are "code islands"—standalone chunks of code that can work independently of the HTML "sea" around them.
PHP scripts are generally saved with the file extension .php to signify their type. Whenever your web server is asked to send a file ending with .php, it first passes it to the PHP interpreter, which executes any PHP code in the script before returning a generated file to the end user. The basic unit of PHP code is called a statement, and ends with a semicolon to signify it is a complete statement. For clarity, one line of code usually contains just one statement, but you can have as many statements on one line as you want. These two examples do the same thing:
<?php
// option 1
print "Hello, ";
print "world!";
// option 2
print "Hello, "; print "world!";
?>PHP purists like to point out that print is technically not a function and, technically, they are correct. This is why print doesn't require brackets ...