This is part 2 of my series, Basics of PHP. In this part of the series, I give you the basic syntax of PHP.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
PHP Tags
PHP code can be included in 4 different tags. The one given in the previous part is the most common and it is the one recommended. I will not talk about the other ones. The one we saw in the previous part is:
This can be considered as a single tag HTML element. The element is not displayed at the browser. The code is executed at the server and the results if any, are sent as HTML content to the browser. If the file at the server is not fundamentally an HTML file, then this tag will start and end the file and the results can be sent elsewhere.
Statement
A statement in PHP is a short piece of code that ends with a semicolon. The only code in the following tag is a statement:
The statement here, begins with the word, echo and ends with the semicolon. The last statement in a block does not need a semicolon. The third statement below does not have a semicolon and that is OK; it is the last statement in a block.
Try the above code (put in the surrounding HTML tags first). If there is only one statement, you also do not need a semicolon. Try the following:
So, if you have only one statement, you can omit the semicolon. If you have more than one statement, you can omit the semicolon for the last statement (not the first).
If you have only one statement and you use a semicolon, there is no problem. If you have many statements and you put a semicolon for the last statement, there is still no problem.
Comments
You should have comments in your code. Comments are not executed. Comments are to remind you later of why you typed a particular piece of code. There are two types of comments: single-line comments and multiple-line comments. A single-line comment can only be in one line; something like:
// This is a single-line comment.
A single-line comment begins with a double forward slash or a hash. For a single-line comment, everything to the right of the comment is not executed. You can start a single line comment with either // or # as in the above and the following example:
# This is a single-line comment.
A multiple-line comment begins with /* and ends with */ . An example is:
/* This is a multiple-line comment. It can be of any length, and
you can put whatever you want here. */
A multiple-line comment spans more than one line. The opening delimiter is a forward slash and asterisk. The closing delimiter is an asterisk and forward slash.
Try the following code:
String
Note that the text that is displayed on the web page is either in single or double quotes. Text in quotes is called a string. The word, echo, is a construct that sends the string to the browser. The content of the string is HTML code (tags). Simple text is still HTML code.
PHP Version
The latest version of PHP is version 5.2.8. These tutorials are based on that.
Let us stop here and continue in the next part of the series.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- PHP Loop StatementsIn PHP you have the do-while loop, the while loop and the for loop. We shall see what all these mean in this article.
- Boolean Logic and PHP ConditionsIn this part of the series we apply Boolean logic to PHP conditions.
- PHP Array In PHP an array is an ordered map where values are associated to keys. I explain all that in this article and how to use a PHP array.
- ActivePerl Basic SyntaxIn this part of the series, I give you the basic syntax of ActivePerl.
- The Principles of Batch Process Program Development: Basic SyntaxLearn how to develop, test, debug, and implement batch process programs on your home computer with The Principles of Batch Process Program Development series.
- C Basic Syntax
- PHP Object Basics
- JavaScript Conditional Statements
- Basics of PHP Variables
- PHP Function Basics
- PHP Error Basics
- PHP Comparison and Arithmetic Operators
