Syntax in PHP

Installation of XAMPP

What is the syntax?

The syntax is a basic structure to write a code. It tells the programmer how to write a code so that our program can be executed properly.

A syntax is made of different symbols like question marks, commas, brackets, etc.

For example, the basic syntax of PHP code is: –

<?php Echo “Text”; ?>

The syntax has to be written as it is defined. If the syntax is not correct. The program will not run and it will be terminated instantly.

In every programming language syntax is the king. So, while writing any code a programmer must be aware of the correct syntax of the code.

*So, now the question arises how will one identify if the syntax is correct or not and if it is not correct then where is the problem?
So, this problem is solved by text editors. Text editors help in identifying errors by highlighting syntax. They also show where the syntax has gone wrong because of which the program is not running.

So, a good text editor is needed to identify errors. For example, Sublime text editor, Visual studio code, Notepad++, etc.

Syntax to write PHP code: –

It starts with <?php and ends with?>

<html> 
<head> 
<title>Hello World</title> 
</head> 
<body> <?php echo "Hello, World! “;?> /*echo is a built-in-function of PHP that enables the data to print on the screen at the time of run*/ 
</body> 
</html>

A PHP script can be placed anywhere in a document. A PHP script is that part of the code which starts with <?php …all the php codes here and ends with?>

Code comments in PHP

Commenting PHP code

Commenting is the text which does not show on a web browser even after it is written on an editor.

Why comments are used?

Comments can be used to:

  • Help others to understand your code better – When multiple programmers work on a single code, they must use comments so that other programmers can understand what work is being done by your fellow programmers.
  • Help to remind yourself what you did last – Even if you come back after a time you don’t forget what is this code about.

So, commenting on your code is as important as your code.

Single line comments- they are used for short explanations. They are used with the ‘#’ and ‘//’ signs. For example,

<? 
# This is a comment, and 
# This is the second line of the comment 
// This is also a comment. 
?>

Multi-line comments- They are used for long phrases or multiple lines. They start with the ‘/’ and end with the ‘/’ sign.

<? 
/* This is a comment with multiline Author: Mohammad Makhteshim Purpose: Multiline Comments Demo Subject: PHP */ 
?>

*Save PHP file.

PHP file is saved with .php extension.

For example, filename.php (programmingtutorial.php)

(Extensions are predefined statements that help the software to recognize the type of file. Every file is saved with some kind of extension according to the software.)

*example .docx, .c, .cpp, .php etc.

PHP is a case sensitive language

The syntaxes of most of the programming languages are case-sensitive. That means there will be a difference between every capital and small letter word. You can write your code in lower cases. However, most of the programming languages are written in lower cases only.

The below program shows that you can print anything in any case it will not affect the output of the program because it does not contain any variable

The input of the program

<html>
<body>
<?php 
echo "Hello world using echo </br>"; 
ECHO "Hello world using ECHO </br>"; 
EcHo "Hello world using EcHo </br>";
?>
</body>
</html>

The output of the program

Hello world using echo 
Hello world using ECHO 
Hello world using EcHo

This example shows that you cannot use different cases when you are working with variables. Because every variable has its different meaning. $car is not as same as that of $CAR

The input of the program

<html>
<body>
<?php
$car = "ferrari"; 
echo "My car is ". $caR ."</br>"; 
echo "I have a ". $car ."</br>"; 
echo "I dream of having a ". $CAR ."</br>";
?>
</body>
</html>

The output of the program.

Notice: Undefined variable: caR in D:\xampp\htdocs\program\p2.php on line 8
My car is
I have a Ferrari
Notice: Undefined variable: CAR in D:\xampp\htdocs\program\p2.php on line 10 I dream of having a

So, in the above examples, if code is written in lowercase or small letters, it will not be confusing and will be easily understandable.

Php Echo and Print

You must be wondering about what is print. Print is a statement that also prints our code on screen like an echo. You can use any of them but with a little consideration taken into account.

The changes which differentiate both of them are explained below.

ECHO & PRINT

  • echo can print multiple strings. print only works with a single string. With multiple strings, it will show an error.

Output with echo

Output with print

  • echo does not return any value while print returns a value of 1.

Output with echo

Output with print

  • echo is faster than print because echo does not have to return any value.
Syntax in PHP
Show Buttons
Hide Buttons