PHP Basic Syntax
The syntax of PHP is very easy to understand compared to some other programming languages.
PHP Tags
- <?php - Opening Tag
- ?> - Closing Tag

When you request a PHP file, the server process the file using the PHP parser. The parsed output will be sent as the response.
When PHP parses a file, it searches for opening and closing tags. All the code inside these tags is interpreted. Everything outside these tags is ignored by the PHP parser.
1. Only PHP
PHP files can have only PHP code. In this case, you can omit closing tags. This prevents accidental whitespace or new lines being added after the PHP closing tag.
2. PHP inside HTML
You can use PHP code inside HTML. Here, the closing tag is compulsory.
PHP inside HTML Example
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>My First PHP-Enabled Page</h1>
<p><?php echo 'Hello World'; ?></p>
</body>
</html>
Run Example ››In the same way, PHP tags can be used inside any text output like HTML, Javascript, JSON, etc.
How does it work?
- PHP returns all the text that are not inside PHP tags without any execution.
- The code inside PHP tags will be executed and the output of that code will be returned.
PHP Statements
In the above examples, we used the echo statement to output a string. echo is a built-in PHP function. In PHP, each statement or instruction is separated by a semicolon ;
In the above example,
- <?php announces the opening of the PHP code.
- echo says to output the string right after it.
- ; says to terminate the current statement or instruction
- ?> announces the ending of the PHP code. (This is not needed in this script as we only have PHP code in it.)
PHP Case-Sensitivity
PHP is a SEMI-CASE-SENSITIVE language which means some features are case-sensitive while others are not.
Following are case-insensitive
- All the keywords (if, else, while, for, foreach, etc.)
- Functions
- Statements
- Classes
All the echo statements below are equal.
PHP Case-Insensitive Example
<?php
echo 'Hello World';
ECHO 'Hello World';
eCHo 'Hello World';
Echo 'Hello World';
Run Example ››In the below example, the second echo statement will throw an error or will output an empty string, because $Name is not defined. (If you don't understand the code, just remember that PHP variables are case-sensitive. We will be discussing more about PHP variables in later tutorials)
To prevent the error, we have commented the statement. What are comments? Click the "NEXT" button to learn about it.