Like any other programming language PHP has a set of syntax rules which we need to follow while programming in the PHP language. Learning syntax rules is important because it will help programmers increase their speed of programming, accuracy and will help them master the language quickly.
PHP files commonly have a .php as file extension and they are always executed on the server.
Tags in PHP or Escaping To PHP
PHP programming logic are written using the PHP tags because PHP scripts can be written in separate files or anywhere in an HTML document. The PHP parsing engine needs a way to differentiate PHP code from other elements. PHP tags help achieve this. This is called 'Escaping to PHP' because the processing must be done by the PHP engine.
We can ‘Escape to PHP’ by using the following 4 methods:
1. Canonical PHP tags
Here PHP scripts start with the tag style <?php....... and end with ?>
Every PHP command ends with a semi-colon [;].
Example: "Welcome to PHP" program
<?php # Here the echo command prints the message echo "Welcome to PHP"; ?>
Output: Welcome To PHP
2. SGML (or) Short HTML Tags
Here PHP scripts start with short or short-open tags which look like
<?....?>
Usage of Short Tags is the easiest way to initialize PHP code and it is very convenient.
However short tags can be used only if the following prerequisites are satisfied:
- Select '--enable-short-tags' configuration option when you are building PHP code so that short tags works.
- Also keep the short_open_tag 'On' in your php.ini file.
Because the short PHP code has issues with parsing of XML file, it is recommended that one doesn't enable the short PHP tags and keeps them disabled.
Here is an example of short PHP code
<?
# Here the echo command works due to
# settings being configured in php.ini and during code build
echo "Learn PHP because PHP is the best";
?>
NOTE: <?= is a syntax that is used as a combination of <?php and echo as the first statement. It is available despite short tags are off.
3. HTML Script Tags
Here the PHP code is enclosed using tags.
Example :
<script language="php">
echo "Learn PHP because PHP is the best";
</script>
The HTML Script tag is actually removed from PHP 7.0 and is no longer supported.
4. ASP Style Tags
PHP code can also be written with ASP style tags. We can use the ASP Style tags by setting the configuration option in the "php.ini" file.
These tags are called ASP style tags because they are also used by 'Active Server Pages’ to describe code blocks.
Example:
<%
# This can only be written because the setting is turned on in php.ini
# to allow %
echo "Learn PHP because PHP is the best";
%>
Like HTML script tag, ASP style tag support is also dropped from PHP 7.0.
Comments in PHP
Comments are statements in a program by developers which describe the intent and purpose of the code. They play an important role in programming because they help make the code more readable and understandable. Comments in PHP code are not read or executed by the PHP engine. Therefore they do not affect the output.
We can use two styles of comments in PHP. They are as follows :
Single Line Comment
As the name suggests these are a single line or short explanations that one can add in their script. in order to add the comment, we need to begin the statement with (//) or (#).
Example:
<?php
// This is a single line comment
echo "Learn PHP because PHP is the best!!!";
# This is also a single line comment
?>
Multi-line or Multiple line Comment
Sometimes longer explanations need to be given in a program because the code may be complex or in order to help the readers gets a lucid understanding of the code. Multi-line comments allow users to write multiple lines of comment by using a single tag /*…………..*/
Example:
<?php
/* Example for multiline comment
In PHP, variables are written
by adding a $ sign at the beginning.*/
$message = "Learn PHP because PHP is the best!";
echo $message;
?>
PHP Syntax is Case-sensitive
In PHP besides variables, all other keywords are not case-sensitive. This includes all keywords, functions and class names. However, the most important point to note is that variables are case-sensitive.
Therefore we have to be very careful while defining and using variables. Let’s look at this example:
<?php
// In the example here we can see that all echo
// statements are executed in the same manner because keywords are case-insensitive
$variable = 100;
ECHO $variable;
// This line will show a RUNTIME ERROR because the case of the variable is different than the one defined
// Hence you will get an "Undefined Variable" error
//echo $VARIABLE
echo $variable;
?>
Output:
The two echo statements produce an output of 100 because echo is not case sensitive.
Blocks in PHP :
Multiple statements can be executed due to some conditions in PHP (based on a condition or a loop). These statements need to be grouped together into blocks. They have to be enclosed within curly-braces ({})because this will help differentiate the block of code from the rest of the program.
Example:
<?php
$var = 200;
if ($var rb 100){
echo (" Positive because it is greater than 0 ");
}
?>
Output:
Positive because it is greater than 0
Statement Syntax
A statement in PHP comprises of an expression followed by a semicolon ( ; ). Therefore a group of valid PHP statements ,which are enclosed within PHP tags, form a PHP program.
The example below is a typical statement in PHP which assigns a string of characters to a variable called $greeting.
$greeting = "Welcome to PHP!";
Running a PHP Script from the Command Prompt
The command prompt can also be used to run PHP scripts. The syntax for running PHP scripts is :
$ php filename.php
Example:
<?php
echo "Welcome to PHP !!";
?>
Now run this script in the command prompt by using the following statement
$ php test.php
Output:
Welcome To PHP !!