PHP Syntax

Created with Sketch.

PHP Syntax

Summary: in this tutorial, you’ll learn basic PHP syntax, including case sensitivity, statements, and whitespaces.

As a programming language, PHP has a set of rules that governs how you write programs.

PHP code

Like HTML, you need to have the opening tag to start PHP code:


Code language: HTML, XML (xml)

If you mix PHP code with HTML, you need to have the enclosing tag:


Code language: PHP (php)

For example:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>PHP Syntax</title>
<h1><?php echo 'PHP Syntax'; ?></h1>

Code language: HTML, XML (xml)

However, if a file contains only PHP code, the enclosing tag is optional:

echo 'PHP Syntax';

Code language: HTML, XML (xml)

Case sensitivity

PHP is partially case-sensitive. Knowing what are case sensitive and what is not is very important to avoid syntax errors.

If you have a function such as count, you can use it as COUNT. It would work properly.

The following are case-insensitive in PHP:

  • PHP constructs such as if, if-else, if-elseif, switch, while, do-while, etc.
  • Keywords such as true and false.
  • User-defined function & class names.

On the other hand, variables are case-sensitive. e.g., $message and $MESSAGE are different variables.


A PHP script typically consists of one or more statements. A statement is a code that does something, e.g., assigning a value to a variable and calling a function.

A statement always ends with a semicolon (;). The following shows a statement that assigns a literal string to the $message variable:

$message = "Hello";

Code language: PHP (php)

The above example is a simple statement. PHP also has a compound statement that consists of one or more simple statements. A compound statement uses curly braces to mark a block of code. For example:

if( $is_new_user ) {

Code language: PHP (php)

You don’t need to place the semicolon after the curly brace (}).

The closing tag of a PHP block (?>) automatically implies a semicolon (;). Therefore, you don’t need to place a semicolon in the last statement in a PHP block. For example:

<?php echo $name ?>

Code language: HTML, XML (xml)

In this example, the statement echo $name doesn’t need a semicolon. However, using a semicolon for the last statement in a block should work fine. For example:

<?php echo $name; ?>

Code language: HTML, XML (xml)

Note that it’s OK if the code may not make any sense to you now because you’ll learn more about them in the upcoming tutorial.

Whitespace & line breaks

In most cases, whitespace and line breaks don’t have special meaning in PHP. Therefore, you can place a statement in one line or span it across multiple lines.

For example, the following code snippets are equivalent:

login( $username, $password );

Code language: PHP (php)



Code language: PHP (php)


  • PHP is partially case-sensitive.
  • PHP constructs, function names, class names are case-insensitive, whereas variables are case-sensitive.
  • A statement ends with a semicolon (;).
  • Whitespace and line breaks don’t matter in PHP; do leverage them to make the code more readable.

Leave a Reply

Your email address will not be published. Required fields are marked *