PHP Basic Syntax
Basics of PHP – Part 2
Forward: In this part of the series, I give you the basic syntax of PHP.
By: Chrysanthus Date Published: 28 Jul 2012
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at email@example.com. That is, contact me for the slightest problem you have about what you are reading.
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.
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:
echo 'I am learning PHP.';
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.
echo 'I am a man.<br />';
echo 'He is a boy.<br />';
echo 'She is a girl.<br />'
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:
echo 'I am a man.<br />'
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.
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:
//Talking about a man.
echo "I am a man.<br />";
#Talking about a boy.
echo "He is a boy.<br />";
/*Talking about a girl.
and only the girls*/
echo "She is a girl.<br />"
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.
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.
Related LinksMajor in Website Design
Web Development Course