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.
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.
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 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;
PHP provides various operators, including arithmetic, assignment, comparison, and logical operators. Example:
$a = 10;
$b = 5;
echo $a + $b; // Outputs 15
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 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');
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 are ordered collections of key-value pairs, defined using square brackets:
$fruits = ['apple', 'banana', 'orange'];
foreach ($fruits as $fruit) {
    echo $fruit . '\n';
}
Strings store text and can be defined using single or double quotes:
$msg1 = 'Hello';
$msg2 = "World";
echo "$msg1 $msg2";
PHP supports object-oriented principles such as encapsulation, inheritance, and polymorphism. OOP enhances code reusability, structure, and maintainability.
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.