PHP Tutorials

PHP Syntax Tutorial: Rules You Need to Know

PHP syntax is simpler than it looks. In this lesson you'll see how statements, semicolons and case sensitivity work, with small PHP examples you can run right away.

Serhat ALPASLAN2 min read

Contents

Php

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

PHP code

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

<?php

The closing tag for PHP code should look like this.

?>

Case sensitivity

PHP is (partially) case-sensitive. Knowing what is case-sensitive will save you from syntax errors.

  • PHP constructs such as if, if-else, if-elseif, switch case, while, do-while.
  • boolean values such as true and false
  • User-defined function and class names.

All of the structures above are not case-sensitive. However, variables are case-sensitive, like $text and $TEXT.

Statements

PHP consists of one or more statements. Statements are pieces of code that do a job. For example, you can think of assigning a value to a variable, or calling a variable or a function. Every statement must end with a semicolon (;).

Summary

  • PHP is partially case-sensitive.
  • In PHP, function and class names are not case-sensitive. However, variables are case-sensitive.
  • Statements must end with a semicolon (;).
  • The spaces and line breaks you leave while writing code in PHP don’t matter. However, the code block you write won’t be readable, so it won’t be pleasant.

You can find plenty of examples and resources on the official website.

Share it if it helped

Write a comment