Learn about the syntax of PHP with examples

Learn about the syntax of PHP with examples

To learn every programming language it is necessary to understand its syntax. For PHP we will start from LEXICAL STRUCTURE which is basically the lowest level of syntax for PHP. Lexical structure defines how we name the variables, the characters we can use or how we can insert comments in the program etc.

PHP is case sensitive and insensitive: The variables which can be defined in PHP are case sensitive. But the names of user defined functions and classes are case in-sensitive in PHP. Moreover the keywords and constructs which are build in PHP are case insensitive as well. An example of keyword is echo:

echo(“My blog is website-school “);

Echo(“My blog is website-school “);

eCho(“My blog is website-school “);

All of the above statements are same.

Since we know that PHP code is executed on server. As a result the plain HTML is sent back to the browser.

PHP Scripting Block:

The php block will always start with .  But if the server supports the shorthand then the php block will start from . The position of PHP block can be anywhere in the document. Since you want your document to provide as much as compatibility as possible so you should use the:



syntax to write the php block.

PHP Statements:

The collection of PHP code that does something is known as PHP statement. For you,  easiest example of PHP statement could be variable assignment such as:

$website = 50;

$demon = 2;

you should note that variable is basically a changing value. You can declare a variable name and type once and can change its value at any point in the program and as many time as you want. In the above statement the website is the variable that you will declare in php and assign it a value of 50. You really need to remember that variable name in PHP starts with $ sign! Similarly, the demon variable has been given a value of 2.

$school = “blog”;

If you look at the above statement then you will notice that the variable school does not have numerical value, instead it has been assigned a word “blog” as a value. You need to remember that the words (or group of words i.e. complete sentences as well) are written inside quotation marks while assigning them to a variable.

Similarly, the complicated example of PHP statement can be loop that has multiple checks or exit points such as:

if ($website==$demon)


echo “both variables are equal”;

echo “YAHOO”;


The above example is a if else loop which will be explained in a loop section in detail. But for now all you need to know is that this loops checks if the value of website and demon are equal. If yes than the output of loop will be a statement:

both variables are equal YAHOO

For now you don’t have to worry about LOOPS. We will get there Learn about the syntax of PHP with examples

Since we are discussing PHP statements, so I want to mention Compound statements. The statements which are enclosed in braces are basically known as Compound statement. Like the echo statements in the above loop are compound statements.

I know that those of you who know nothing about programming are wondering what is the purpose of semicolon? First I want to introduce you to comments in PHP code.

PHP Comments:

The comments about the code can be written after each statement by putting // after the end of every statement. You can either do that or you can put your comments between /* comment*/ to insert a large block of comments in the code.

Now we will move on to semicolon!

PHP Semicolon:

The semicolon in PHP statements is used to separate the single statements. It also act as a separator from one group of statements to another set of statements. The compound statements do not require the semicolon after a closing brace of any loop or conditional test. But in the single compound statement the semicolon is must. For example, if you see this example again:

if ($website==$demon)


echo “both variables are equal”;     // semicolon is compulsory here.

echo “YAHOO”;    // semicolon is compulsory here.

}  // semicolon is not required here.

Now I think that you can easily recognize the comments after each statement. If you are writing a PHP code, then you can easily skip the semicolon after the PHP tag because using a semicolon after a PHP tag is optional.


if ($website==$demon)


echo “hi”;


echo “YAHOO”    //semicolon is optional after this tag


But for good programming practice, You need to remember that semicolon are must at the end of every single statement, they are not use at the end of compound statement end brace and they are optional after PHP tag. But you should put them after PHP tag so that you don’t get confused.

To get a mobile app for your business visit www.codescamp.com

Book a free consultation today!


5 Responses to Learn about the syntax of PHP with examples

  1. Luigi Fulk says:

    I simply want to tell you that I’m new to weblog and honestly loved your web-site. Almost certainly I’m likely to bookmark your site . You absolutely come with good articles. Bless you for sharing your website.

  2. ʜi there! Τhis pߋst could not be written much better!

    Reading through this post reminds me оf my previߋus
    roommate! He constantly kеpt preaching about this. I’ll send this post to
    him. Fɑirly certain he’s going to have a great reаd. I appreciɑte
    you for sҺaгing!

  3. ntc 405 says:


    While checking out DIGG yesterday I noticed this

  4. It really is almost unthinkable to find well-educated users on this theme, and yet you come across as like you realize those things you’re preaching about! Gratitude

  5. Hello my friend! I want to say that this post is amazing, nice written and come with approximately all important infos. I’d like to see more posts like this .

Leave a Reply