Current Location: Home> Latest Articles> Complete Guide to PHP Syntax: Basic Rules and Common Language Structures Explained

Complete Guide to PHP Syntax: Basic Rules and Common Language Structures Explained

M66 2025-11-01

Overview of PHP Syntax

PHP is a simple and easy-to-learn server-side scripting language. Its syntax is based on the C language but is more flexible. Widely used in web development, PHP efficiently handles forms, interacts with databases, and generates dynamic web pages.

Data Types

PHP supports multiple data types, including strings, integers, floats, booleans, arrays, and objects. Developers can choose the appropriate data type based on different requirements for storing and processing data.

Variables

In PHP, variables start with a dollar sign ($). A variable name must begin with a letter or underscore and cannot start with a number. For example:

$name = 'Tom';
$age = 25;

Variables do not require explicit type declarations; PHP determines the type automatically based on the assigned value.

Constants

Constants store values that cannot be changed during script execution. They are defined using the define() function and are usually written in uppercase letters. Example:

define('SITE_NAME', 'MyWebsite');
echo SITE_NAME;

Operators

PHP provides various operators, including arithmetic, assignment, comparison, and logical operators. Example:

$a = 10;
$b = 5;
echo $a + $b; // Outputs 15

Control Structures

Control structures manage the flow of program execution, including conditional and looping statements. Example:

if ($age >= 18) {
    echo 'Adult';
} else {
    echo 'Minor';
}

Common loops in PHP include for, while, and foreach.

Functions

Functions are reusable blocks of code that can accept parameters and return results. Defined using the function keyword:

function greet($name) {
    return 'Hello, ' . $name;
}

echo greet('Alice');

Classes and Objects

PHP supports object-oriented programming, allowing developers to organize code through classes and objects. Classes are defined using the class keyword, and objects are created with new:

class Person {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
    public function sayHello() {
        return 'Hello, ' . $this->name;
    }
}

$user = new Person('Tom');
echo $user->sayHello();

Arrays

Arrays are ordered collections of key-value pairs, defined using square brackets:

$fruits = ['apple', 'banana', 'orange'];
foreach ($fruits as $fruit) {
    echo $fruit . '\n';
}

Strings

Strings store text and can be defined using single or double quotes:

$msg1 = 'Hello';
$msg2 = "World";
echo "$msg1 $msg2";

Object-Oriented Programming (OOP)

PHP supports object-oriented principles such as encapsulation, inheritance, and polymorphism. OOP enhances code reusability, structure, and maintainability.

Other Common Features

In addition to its basic syntax, PHP offers rich functionality such as error handling, file operations, session management, and database interaction. These features make PHP a powerful tool for building dynamic web applications.

By mastering these basic syntax elements, developers can quickly start PHP development and build a solid foundation for learning frameworks and advanced applications.