Published on Apr 16,2015
1.8K Views
Email Post

This tutorial focuses on how to differentiate PHP code in a HTML script. Before entering the topic, let’s look at brief introduction to what PHP is. With the development of Internet Technologies, the use of online business is increasing rapidly. Today most of the business contracts is conducted online. Therefore most of the business websites use the latest technologies to make their websites look effective. We use various languages to build these websites like HTML, PHP, etc. PHP is a server side scripting language used by most of the website developers to improve the appearance and functions of the websites. PHP is mainly used to create dynamic web pages. PHP can be easily embedded in the HTML script and therefore it is widely used.

Now let us see various aspects of PHP.

Syntax for PHP:

The PHP syntax is similar to the syntax of C language. There are four points to be considered when writing the syntax for PHP. They are:

  • PHP is white space insensitive
  • PHP is case sensitive
  • Statements are terminated by semicolons
  • Braces make blocks

Let’s look at them in detail.

#1. PHP is White Space Insensitive

Whitespaces, tabs and carriage returns are the stuff which we type that is typically invisible on the screen. PHP never matters about the whitespace characters.

Example:

<?php
 $four=2  + 2;//single spaces
$four <tab>=<tab>2<tab>+<tab>2; //spaces and tabs
$four =
2
+
2; //multiple lines
?>

#2. PHP is Case Sensitive

PHP distinct between lowercase and upper case letters and Variables are case sensitive.

Example:

<?php
$case_sensitive = 88;
Print(“The value of the variable is $case_sensitive”);
Print(“The value of the variable is $CaSe_SENSITIVE”);
?>

Output:

The value of the variable is 88

The value of the variable is

The first print statement prints the value as ’88’ and the second print statement prints nothing because there is no variable ($CaSe_SENSITIVE) assigned in this script.

#3. Statements are Terminated by Semicolons

All the statements in the PHP program must be terminated in order to run the program successfully.

  • A statement in PHP or any expression is followed by semicolon “;”

  • And the semicolon is the full stop at the end

Example:

<?php
$greeting = “Welcome to PHP!”;
?>

#4. Braces Make Blocks

When statements are to be combined to perform a particular task, the sequence of statements are wrapped in between the curly braces.

Example:

<?php
 If (3 == 2 +1)
 Print(“ Good – I haven’t totally lost my mind”)
If (3 == 2 + 1)
{
     Print(“Good – I haven’t totally”);
      Print(“lost my mind”);
}
 ?>

In order to build complex web pages, PHP code is combined with the HTML code. You will have to differentiate the PHP code from the HTML code so that the PHP Parser can interpret the PHP code.

Let’s look at how to do this exactly. 

Differentiating the PHP code in the HTML Script Escaping in to PHP

Let’s look at the different ways in which the PHP code can be differentiated from the HTML code so that it becomes easier for the PHP ParHow to differentiate the PHP code in the HTML scripts to interpret the PHP code. Here, Escaping is done in order to make it easy for people to differentiate PHP script from the HTML script. Escaping to PHP is nothing but differentiating PHP code from other elements.

This can be done in four ways:

  • Canonical PHP tags

  • Short-open (SGML – style) tags

  • ASP – style tags

  • HTML Script tags

Canonical PHP Tags

Canonical PHP tags are the simple way of differentiating the PHP code from the HTML code. Here the PHP code is embedded as shown below:

Syntax:

<?php
//php code
?>
Example:
<html>
<head>
<title>PHP script Example</title>
<body>
<?php echo "Welcome to PHP Script"?>
</body>
</html>

Short-Open Tags:

Short open tags can be defined as ‘<? //php code ?>’. To use, short tags we should follow any one of below steps:

  • Choose the -enable-short-tags configuration option when we are building PHP

  • Set the short_open_tag setting in our php.ini file and set it to on

Syntax:

<?
//php cod
?>
Example:
<html>
<head>
<title>PHP script Example</title>
<body>
<? echo "Welcome to PHP Script"?>
</body>
</html>

ASP-Style Tags:

ASP stands for Active Server Page. The ASP style tags can be used in PHP only if this feature is enabled in the PHP configuration file, php.ini. This method is very rarely used.

Syntax:

 <%
 //php code
%>
Example:
 <html>
<head>
<title>PHP script Example</title>
<body>
<%    "Welcome to PHP Script"    %>
</body>
</html>

HTML Script Tags:

Here, we specify the PHP code in the HTML document by specifying the code in the <script language=”    “> tag.

Syntax:

<script language="PHP">
//Some code here
</script>
Example: 
<html>
<head>
<title>PHP script Example</title>
<body>
 <script language=”PHP”>
"Welcome to PHP Script"
</script>
</body>
</html>

Storing information is one of the basic aspects of any programming language. Sometimes you may also have to store information in the PHP script. This is done by assigning a value. As soon as a value is assigned, a variable is created. So to store the values, we use Variables.

Got a question for us? Please mention them in the comments section and we will get back to you.

Related Posts: 

Get Started with PHP & MySQL.

Share on

Browse Categories

Comments
2 Comments