PHP Syntax


In this lesson, we will teach you the basic syntax of PHP language. PHP files are saved with .php extension. Inside a PHP file, you can write HTML code just like you do in regular HTML pages as well as embedded PHP code blocks to execute on the server-side. PHP is a quite simple language with syntax similar to C and C++. It is also very flexible, but there are a few syntax rules you need to learn.

PHP Tags

When an interpreter parses a file, it looks for PHP opening and closing tags, which are <?php and ?> which tell PHP that it has to interpret this code block into HTML from PHP. This behavior allows PHP code to be embedded in other documents, as everything outside these tags will be ignored by the PHP parser.

Since PHP 7.0 the only valid tags are <?php and ?>

Tip: The closing tag at the end of the file can be omitted if a file contains only PHP code.

Example of PHP Tags

<?php 
	echo “This is my first PHP program”;
// the script ends here 
?>

PHP Comments

A comment is a piece of code which isn’t supposed to be executed. The main purpose of providing comments is to assist developers to make the code more readable and understandable. Comments are vital if you are working in a team. With proper comments, you can also document the process of coding.

Types of Comments

PHP supports two types of comments:

Single Line comments – The one-line comment starts with // or #. Anything written after these symbols till the end of the line will be ignored by the PHP parser. These type of comments are usually written to explain variables, constants or expressions.

Example of Single Line Comment

<?php 
//this is a single line comment
//this is another comment

echo “Lets learn some commenting!”;
?>

Multi-Line Comments – As the name suggest these will extend comments to many lines. Multi-line comment starts with /* and ends at */. Anything between these two symbols will be ignored by the PHP parser. These are mostly used to explain the document or user-defined functions or classes.

Example of Multi-Line Comment

<?php

/* This is a multi-line
    Comment */
echo “this is how multi-line comments”;
?>

Case Sensitivity

PHP is flexible when it comes to case-sensitivity. This includes all types of spaces (tabs, spaces, and carriage return). This means that PHP will ignore all the spaces or tabs in a single row.

Unlike many programming languages, reserved words, function names are case insensitive. For example, both these line will be interpreted in HTML without any error.

Example of Case Sensitivity in PHP

<?php 
Echo “this is valid”;
echo “this is also valid”;
?>

However; all variable names are case-sensitive which means $sum is not equal to $Sum.

Tip: Despite PHP’s flexibility, try to stick to one style and follow that style throughout the project. This would help to maintain consistency in the code.

PHP Statements

Every PHP statement ends with a semicolon, like this: $a = 5;

Missing a semicolon would make PHP treat multiple statements as one statement, which will produce a parsing error. To declare variable $ symbol is used in front of all variable names. Variables are memory location which stores data to be used in the program. To store characters, use a single quote and to store strings you can use single or double quotes.

Example of Statment in PHP

<?php 

$ch = ‘c’;
$str = “String “;
$num1 = 10;
$num2 = 10.5;
$sum = $num1 + $num2;
?>

$num1 + $num2 is an expression that will be evaluated to a value and then assigned to the variable on the left side.

PHP Code Blocks

Like other programming languages, the related lines of code are placed inside curly brackets which form a block of code. This block of code could be under a single condition or loop.

Example of Code Block in PHP

<?php 

If(true)
echo “PHP is a scripting language”. “<br/>”;
echo “It is really easy to learn PHP”;

?>

The dot operator used in the above statement echo “PHP is a scripting language”. “<br/>” is called a concatenation operator. It is used to concatenate two strings together.

Introduction to PHP Tutorial Home PHP Variables
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →