Introduction to PHP

PHP is one of the first growing, free and platform independent server side scripting language

Introduction to PHP

What is PHP?

PHP stands for Hypertext Preprocessor. PHP is one of the first growing, free and platform independent server side scripting language. PHP is very easy to understand and user friendly programming language. The first version of PHP was released in 1994 by Rasmus Lerdof. 
 

Sample PHP code

<?php
    echo "This is my first line.";
?>

 

Lets examine this code line by line:

1. The first line is consist of opening of tag, <?php. And it is mandatory.

2. The second line has echo statement, which is used to display text in browser when we run the php file. All string in PHP should pass between single quote or double quote. The second line ends with a semicolon. You must use semicolon after each statement ends. The semicolon is known as the terminator in PHP. Otherwise it will display error when you run the code.

3. The third line consist of the closing tag, ?>.The closing tag marks the end of the PHP code and it is mandatory.
 

How to Embed PHP in HTML

Lets discuss how to embed PHP codes in HTML page. 
 

<!DOCTYPE html>
<html lang="en">
<head>
<title>My First PHP Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
   echo "This is my First PHP Page.";
?>
</body>
</html>

 

PHP parse error

A parse error is come across when the code has syntax errors in it .For example, each line of PHP code should end with a semicolon. If you don't specify a semicolon, you will get parse error, and the code will not be executed. Similarly, you will get a parse error when you forgot to uses double quotes for strings. Apart for this, there are several others options that might display parse error. The PHP parser tells the exert line on which it encounters a parse error. So you can easily find the mistakes and rectify it.

The following code will display error, because it is missing the semicolon termination.
 

<?php
    echo "This is my first line."
?>

 

Escaping PHP Code

Escaping code is the process of overlooking instances of special characters in the code. The following code will display error. To avoid the error you can use an escape character (\) before double quote. 

Wrong Code

<?php
  echo "Do you think, "You like php?"";
?>

 

Correct Code

<?php
  echo "Do you think, \"You like php?\"";
?>

 

Comment in PHP code

Comments are an primary part of any program. They are statement that are not executed along with the code. They help developers remember and recall why they have used a particular code block in a script. 
There are four ways of specifying comments in PHP code:
 

<!-- You can put comment here --> 


This is an HTML comment. Comments stated like this will be ignored by the browserand will not be interpreted.
 

//Put comment here. 


This kinds of comments is commonly used within PHP codes.
 

#Put comment here.


The hash character can also be used to specify comments within PHP code.
 

/* 
Put comment here 
Here you can comment multiple lines    
*/