When a user visits a PHP page (that ends with .php extension), the browser requests the page to the web server. The web server forwards the request to the PHP interpreter. PHP interpreter understands PHP syntax. It reads the file, and processes it to generate HTML document. Here you’ll learn the basic syntax used in PHP.
A simple PHP script
At first let’s take a look at a simple HTML script. If you know HTML (which is recommended before learning PHP), you’ll find it familiar.
<! DOCTYPE html> <meta charset="”utf-8”" /> Hello World
If you view the above file in a browser, you’ll find the following output.
Output:
Hello World
To display the same output in PHP, PHP code (line 8 to 10) would be-
<! DOCTYPE html> <meta charset="”utf-8”" /> <!--?php <br ?--> echo “Hello World”; ?>
Now let’s examine the above PHP code (line 8-10) in detail.
PHP tags
All the PHP codes are written between starting and closing PHP tags. Starting and closing tags pair can be written in four ways-
- <?php ?>: All PHP codes must be written within <?php and ?> tags. In the above example, the PHP code in line no. 9 is placed within <?php and ?> tags.
- <script language=”php”> </script>: PHP codes written within <script language=”php”> and </script>.
- <? ?>: This is the shorter version of (<?php and ?>). But it is not recommended to use unless it is enabled in php.ini configuration file.
- <% %>: All PHP codes written within <% %> tags. Again, it is not recommended to use unless it is enabled in php.ini configuration file.
When PHP interpreter receives a .php file, it looks for the one of the starting PHP tag listed above to start interpreting PHP statements and continues until it finds its closing PHP tag.
Recommended Usage:
<?php ?> tags are most commonly used, hence, recommended.
PHP statement
- A statement is the smallest standalone element of a language. In the above example, line no. 9 is a statement.
- A PHP code may contain one or more statements.
- A statement usually ends with a semicolon.
Displaying output in a browser
There are 2 ways to display output –
- echo: To display output in PHP, echo is used. In the above example in line no. 9 echo is used to display “Hello World”. Please note when you try to display string in PHP, you need to enclose the whole string with double quotes (as the above example shows) or single quotes, but not the combination of both (more on this later).
- print: print is also used to display output.
PHP tags (<?php and ?>) placement
- Generally, PHP tags are embedded within the context of HTML (This is why PHP is called embedded language.). The above is a example of it.
- You can also write the PHP code in a separate file.
PHP statement termination
Each PHP statement is terminated by a semicolon (;). In the above example, at the end of line no. 9, semicolon is used to end the line. It indicates the end of that statement.
Comments in PHP
- Comment is a smart way to document in plain English what code does.
- Adding comment is a good programming practice.
- PHP interpreter ignores to process it and remove it when it generates HTML output from PHP code.
- As PHP interpreter ignores comments, those are not visible in browser’s source code.
Why you should care comments?
- No matter how well you know your code today, it is natural that after 2 weeks or six months you’ll have no idea what the code does. Well commented script can help you remind everything.
- If you work in a team and forward one of your scripts that are not commented to another programmer, he’ll not understand your coding easily.
How to add comments?
There are three ways to add comments-
- One-line C++ style comment:
This is the most commonly used way to add single line comment. It looks like two double forward slashes (//). After //, everything is ignored of that line to the end. - Multi-line C style comment/Block comment:
This type of comment starts with /* and ends with */. Anything between those are ignored by PHP interpreter.Programmers use this comment type to temporarily comment a portion of codes to find out bugs. - One-line Unix shell-style comment:
This type of comment starts with #(hash/pound) symbol. After #, everything is ignored of that line to the end.Many programmers use this to comment the introductory block of each script to notify purpose of the script, author name, author’s contact, creation date, modification date. Check the following example to see it in action.Comment Examples
<! DOCTYPE html> # Author: Neil Fin # Script: Comments in PHP # Creation Date: 20th January, 2013 <meta charset="”utf-8”" /> <!--?php <br ?--> /* This is a multi-line comment */ echo "Hello World"; // This is a single line comment ?>
PHP file extension
When you save a PHP file add .php as extension at the end of the file name. Example index.php. contact.php etc.
Next Lesson: PHP Variables ›› |