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.
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:
echo “both variables are equal”;
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
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.
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!
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:
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.
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!