PHP Syntax and Comments


August 23, 2021, Learn eTutorial
1656

PHP programs are executed on the server and the result will be sent back to the browser as plain HTML. PHP scripts are easy to learn for beginners. PHP syntax is simple and not complex like many other languages. 

Basic PHP Syntax

PHP script can be written anywhere on a PHP file. To indicate the stating and the ending of the PHP script “<?php” “?>” are used 

Sample:


<?php
 //PHP code 
?>
 

In the above sample program, we can see that is used to indicate the ending of the PHP script.

The file extension of the PHP file is “.php”. If any PHP file doesn’t contain any other scripts or languages, it is not necessary for the ending tag (?>). A PHP file mostly contains HTML tags and PHP scripts.

Sample Program with HTML and PHP


<html>
  <body>
    <h1>
      <?php echo "Hello World!"; ?> 
    </h1> 
  </body> 
</html>

Statements in PHP are Terminated by using Semicolon

Like any other programming languages in PHP semicolon is used to terminate every statement. Every line of code inside the PHP script tags are PHP statements.

Example:


<?php
 $name = “Robin”;
 $x = 5 + 7;
?>
 

In the above sample program, we can see two statements and each statement is terminated by using a semicolon.

Is PHP Case Sensitive?

Almost all the languages are either full case sensitive or fully case insensitive but in the case of PHP, it is not like that. In PHP the keywords, Classes, and Functions are not case sensitive but on another hand, the variables are case sensitive.


<?php
 echo “Hello World!”;
 ECHO “Hello World!”;
?>

 

Output:


Hello World!
Hello World!

Example2:


<?php
 $welcome = “Welcome to PHP Tutorial”;
 echo “Hello! $welcome”;
 echo “Hello! $Welcome”;
?>

 

Hello! Welcome to PHP Tutorial
Hello! 

In Example 1 we can see that the keyword echo is written in both small and capital and it doesn’t affect the program. But in Example 2 the variable name is different in the second reference so it shows as an undefined variable and doesn’t print the value of the variable.

Is PHP Whitespace Insensitive?

Yes, PHP is whitespace insensitive. Whitespaces like blank space, tabs, and end-of-line characters won’t affect the program in any manner. Indentation is not required in PHP. Programmers can add or remove blank space according to their needs.

Example


<?php 
 $x=10+20;
$y = 10 + 20;
 $z = 10 + 20;
 $u =
 10 +
20;
?>

 

In the above example, all the statements are written with different indentations and all the statements will be executed similarly.

Braces make blocks in PHP

In PHP braces are used to enclose a set of statements. Using braces, we can write the sequence of the statements anywhere in the program.

Example 


<?php
 $age = 20;
 If ($age >= 18) {
  echo “You are eligible for voting”;
 }
 else{
  echo “You are not eligible for voting”;
  echo “Wait till your chance comes”;
 }
?>


 

PHP Comments

PHP comments are used by the developers to describe the statement lines so they can be readable for other developers who look into the code in the future. The comments won’t be executed while running the program; they will be ignored by the interpreter. 

In PHP there are two ways of commenting formats:

  1. Single-line Comment
  2. Multi-line Comment

Single-Line Comment

Single-line comments are used to comment single lines; it is mainly used to explain or describe a line of statements. In PHP single-line comment can be applied in two ways by using // (double forward slash) and #(hash).

Example


<?php
 $x = 5 + 8; // it is for addition 
 $name = “Thor”; # it is a string
 echo $x;
 echo $name;
?>
 

Output:


13
Thor

Multi-Line Comment

Multi-line comments are used to comment on multiple lines. This is mostly used to comment on the whole description of a section or block of a program. In PHP multi-line comment by enclosing the comment in between /*   */.

Example
 


<?php
 /*
 Anything written
 in between 
 this won’t be executed
 */
 echo “Welcome All”;
?>
 

Output:


Welcome All