DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

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

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 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


<?php 
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>
<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 this way, PHP tags can be used inside any text output like HTML, Javascript, JSON, etc.

How does it work?

  • PHP returns all the text is 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


<?php 
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


<?php
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


<?php

$name = 'Hyvor Developer';

echo $name;
echo $Name;

Run Example ››
5
414