PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP HTTP & HTTPSPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Basic Syntax

'Syntax' is the structure of statements in a computer language.

The syntax of PHP is very easy to understand compared to some other programming languages.

PHP Tags

  • <?php - Opening Tag
  • ?> - Closing Tag
PHP Tags
PHP Tags

When you request a PHP file, the server process the file using the PHP parser. The parsed output will be sent as the response.

A parser is a computer programme that makes your code work.

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.

PHP also has a short opening and closing tags <? and ?> which we do not cover in this tutorial. Also, we do not recommend to use them. Always use the above tags.

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.

Only PHP Example

echo 'Hello World'; 

Run Example ››

2. PHP inside HTML

You can use PHP code inside HTML. Here, the closing tag is compulsory.

PHP inside HTML Example

<!DOCTYPE html>
	<title>Hello World</title>
<h1>My First PHP-Enabled Page</h1>

<p><?php echo 'Hello World'; ?></p>


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 ;

PHP Statement Example

echo 'Hello World'; 

Run Example ››

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
You will learn more about functions and classes later.

All the echo statements below are equal.

PHP Case-Insensitive Example

echo 'Hello World';
ECHO 'Hello World';
eCHo 'Hello World';
Echo 'Hello World';

Run Example ››
PHP Variables are case-sensitive.

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)

PHP Sensitive Example


$name = 'Hyvor Developer';

echo $name;
// echo $Name;

Run Example ››

To prevent the error, we have commented the statement. What are comments? Click the "NEXT" button to learn about it.

Visit PHP Help group to get some help from experts.
Profile Picture
Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.
My Websites