PHP Basic Syntax

PHP basic syntax consists of the following:

  • PHP tags
  • Embedded with HTML
  • Terminated by semicolon
  • Comments

The file extension of PHP is .php

Example

PHP Tags

Every PHP programming file should have the following two tags:

  • Open tag – <?php
  • Close tag – ?>

A PHP program should be written between the open and close tag.

Instead of open tag, the short open tag (<?=) might be used. If we want to use short open tag we should change the INI setting in php.ini file like below:

Example

Embedded With HTML

PHP file may or may not contain HTML contents. The presentation or view (one of the MVC) part should contain HTML section.

Example

Terminated by semicolon

Every line between PHP open and close tag should be terminated by semicolon(;). If the open and close tags ended at the same line then the termination of semicolon is optional.

Following are valid examples

Run Above PHP Code

Following are invalid examples

Comments

Comments in PHP is same as C, C++ and Unix shell. The commented text or information will not be executed. The comment will describe the coding or functionality.

There are two types of comments available

  • Single line comment
  • Comment multiple lines

Single Line Comment

This type of comment will comment the single and same line from start to end.

Syntax

We have to put // before the text to be commented.

Example

Comment Multiple Lines

This type of comment will comment both single and multiple lines.

Syntax

The content between/* and */ will be commented and will not provide any output.

Example

Usage of comment

A good programmer should always write a comment in every PHP file and ensures that anyone can understand the functionality of the project and can able to continue the extension of the project without any one’s assistance.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *