PHP Syntax

PHP script can be placed anywhere into the document with the PHP tags along with the HTML.

Default extension for php file is “.php”.

It uses built in php function “echo” to get the text or sentence as output.

In php, any statement followed by semi colon (;).

PHP is start with <?php and ends with ?> which tells php to start and stop interpreting the code between them.

<?php and ?> delimiters are used to tells that the code between them are treated as PHP code.

There is a escaping technique which is used for separate a normal HTML code from the PHP code. This mechanism known as the escaping to PHP.

There are different ways for escaping to PHP.

Some ways are already knows but some rare methods are need to change your php.ini file configuration. ie. short-open or asp style tag.

This tags are also used to combine HTML code into PHP file.

Here we have the various tags which are used for escaping to PHP.

Canonical PHP Tags

The widely used tag for declare PHP is canonical php tags.

This tag is starts from <?php and ends with ?>.

This tag treat the PHP engine that the code within them is declare as PHP code.

echo command is used to tell some data or sentences as output.

In PHP, every command ends with semicolon (;).

Take a look of canonical php tag below :

<?php 
   # Here echo command is used
   echo "Hello, Demonuts!"; 
?> 

We get the result as below :

Hello, Demonuts! 

Short-open (SGML-style) tags

These tags are the shortest and easiest way to represent PHP code.

Short open tags are available if this tag is enable into the php.ini configuration file on the server.

Make the change of short_open_tag setting ON into php.ini file.

Also choose the –enable-short-tags setting option while building your PHP.

This code initialize with <? starting and ending with ?> tag.

Take a look of short-open tag below :

<?  
   # echo command will only work if 
   # setting is done 
   echo "Hello, Demonuts!"; 
?> 

We get the result as below :

Hello, Demonuts! 

HTML Script tags

These tags are implemented using script structure.

Into the latest release of PHP 7 this option was removed so it is no more usable.

Take a look of HTML script tag below :

<script language="php"> 
   echo "hello demonuts!"; 
</script>

We get the result as below :

hello demonuts!

ASP style tags

ASP style tag are used on active server pages for represent the code block.

For use this code block we need to change php.ini configuration file.

This tag starts with <% and ends with %>.

Take a look of ASP style tag below :

<%  
   # Can only be written if setting is turned on 
   # to allow % 
   echo "hello demonuts !"; 
%> 

We get the result as below :

hello demonuts !

CASE SENSITIVITY

In PHP, keywords(e.g. echo, if, else, while), classes, functions ,user-defined functions are not case sensitive.

PHP is not sensitive of white space.

In PHP code, all type of space like tab,spaces etc are not visible in the screen.

PHP takes multiple lines as a single command unless the semi colon (;) define.

EXAMPLE

<!DOCTYPE html>
<html>
<body>
<?php 
   // PHP code is not sensitive for whitespace 
   $var1         =     "hello"; 
   $var2 = 
     "Demonuts "; 
   $result = $var1
   . 
   $var2; 
  
   // "</br>" for new line 
   echo $result, "</br>"; 
  
   $result = $var1 . $var2; 
   ECHO  $result, "</br>"; 
   Echo $result ;
?>
</body>
</html>

Result :

helloDemonuts
helloDemonuts
helloDemonuts 

On the other side the variables are case sensitive in PHP.

Example :

<!DOCTYPE html>
<html>
<body>
<?php
   $language = "PHP";
   echo "My favourite lanuage is " . $language . "<br>";
   echo "Most popular language is " . $LANGUAGE . "<br>";
   echo "Widely used language is " . $LanGuagE . "<br>";
?> 
</body>
</html>

Result :

My favourite lanuage is PHP
Most popular language is
Widely used language is 

In the above example, variable $language, $LANGUAGE and $LanGuagE treated differently.

All the variable doesn’t give same output.

COMMENTS on PHP page

Comment into the declaration of code is only for reference who looking for the code.

Commenting portion of the program is not execute during the execution of file as the part of program.

Comments are used only for understand your code who is refer the code.

Comments are not the part of the program.

It makes program more easy and understandable for other users.

Comments are very helpful in the huge files.

Declaration into comment is not show as the result.

In PHP, we declare comments in two ways.

Single Line Comment

Single line comments starts with double slash (//) or hashtag (#).

Generally used for short description into code.

As per the name this type comment suggest only one line of explanation into the code.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>
<?php
   // single line comment
   # also define single line comment
   echo "Hello Demonuts !";
?> 
</body>
</html>

Result of single line comment as below :

Hello Demonuts ! 

Multi-line or Multiple line Comment:

Multi line comment is used to comment more than one line of description into the code.

Only single tag can extend multiple lines for comment.

Multiple line comment is define by /* ……*/. Between then the declaration is written.

EXAMPLE :

<!DOCTYPE html>
<html>
<body>
<?php
   /* here we define
   multiple lines for
   comment */
   echo "Hello Demonuts !";
?> 
</body>
</html>

Result of multiple line comment as below :

Hello Demonuts !