# Learn PHP Basics

1. **What is scope resolution operator in PHP?**  
    The Scope Resolution Operator in PHP, also known as the "double colon" or `::`, is a token that allows access to static, constant, and overridden properties or methods of a class. It provides a way to refer to elements within a class's scope without needing an instance of that class.
    
    **Key uses of the Scope Resolution Operator:**
    
    * **Accessing Static Properties and Methods:** You can call static methods or access static properties directly using the class name followed by `::` and the method/property name.
        
    
    ```php
        class MyClass {
            public static $staticProperty = "Hello";
            public static function staticMethod() {
                return "World";
            }
        }
        echo MyClass::$staticProperty; // Output: Hello
        echo MyClass::staticMethod(); // Output: World
    ```
    
    * **Accessing Constants:** Class constants are accessed using the class name followed by `::` and the constant name.
        
    
    ```php
        class MyClass {
            const MY_CONSTANT = "PHP";
        }
        echo MyClass::MY_CONSTANT; // Output: PHP
    ```
    
    * Accessing Overridden Properties or Methods (using `parent`): Within a child class, you can use `parent::` to call a method or access a property from its immediate parent class, even if it has been overridden in the child class.
        
    
    ```php
        class ParentClass {
            public function sayHello() {
                return "Hello from Parent!";
            }
        }
    
        class ChildClass extends ParentClass {
            public function sayHello() {
                return parent::sayHello() . " And from Child!";
            }
        }
        $obj = new ChildClass();
        echo $obj->sayHello(); // Output: Hello from Parent! And from Child!
    ```
    
    * Referring to the Current Class (using `self`): Inside a class, `self::` can be used to refer to static properties, static methods, or constants defined within that same class.
        
    
    ```php
        class MyClass {
            const GREETING = "Greetings!";
            public static function getGreeting() {
                return self::GREETING;
            }
        }
        echo MyClass::getGreeting(); // Output: Greetings!
    ```
    

**Properties can also have different levels of visibility, controlling where they can be accessed**:

* `public`: Accessible from anywhere (inside and outside the class, and by child classes).
    
* `private`: Accessible only within the class where they are defined.
    
* `protected`: Accessible within the class where they are defined and by its child classes.
    

## What is a Variable in PHP?

A **variable** is a symbolic name that stores data. In PHP, variables always start with a **dollar sign (**`$`) followed by the variable name.

Example:

```php
<?php
$name = "Jahid";   // String
$age = 25;         // Integer
$isStudent = true; // Boolean
?>
```

Here:

* `$name` stores a **string** (`"Jahid"`)
    
* `$age` stores an **integer** (`25`)
    
* `$isStudent` stores a **boolean** (`true`)
    

---

## Rules for Naming Variables

1. A variable must start with a **$** sign.
    
2. The first character after `$` must be a **letter** or an **underscore (\_)**.
    
3. Variable names are **case-sensitive** (`$Name` and `$name` are different).
    

✅ Valid:

```php
$myVar = 100;
$_value = "Hello";
$Name = "PHP";
```

❌ Invalid:

```php
$1name = "Wrong";  // cannot start with a number
$my-var = 20;      // cannot use a hyphen
```

---

## Variable Structure in PHP

In databases like **MySQL**, you define data types such as `VARCHAR`, `INT`, `BOOLEAN`, etc. In PHP, however, variables are **loosely typed**. That means you don’t have to declare the data type explicitly.

PHP automatically decides the type based on the value you assign. Let’s compare this to database structures.

---

### 1\. VARCHAR (Variable Character String) → PHP String

* **Database side**: `VARCHAR(255)` means it can hold up to 255 characters, but uses only as much space as needed.
    
* **PHP side**: Strings are sequences of characters enclosed in **single quotes (' ')** or **double quotes (" ")**.
    

Example:

```php
<?php
$greeting = "Hello, World!";
echo $greeting; // Output: Hello, World!
?>
```

🔹 PHP manages memory dynamically for strings, just like how `VARCHAR` works in databases.

---

### 2\. INT (Integer) → PHP Integer

* **Database side**: `INT` stores whole numbers.
    
* **PHP side**: An **integer** is a whole number, positive or negative, without decimals.
    

Example:

```php
<?php
$age = 30;
$year = -2025;
echo $age + 5; // Output: 35
?>
```

---

### 3\. DECIMAL / FLOAT → PHP Float (Double)

* **Database side**: `DECIMAL` or `FLOAT` is used for numbers with decimal points.
    
* **PHP side**: A **float** stores numbers with fractions or decimals.
    

Example:

```php
<?php
$price = 99.99;
$pi = 3.14159;
echo $pi * 2; // Output: 6.28318
?>
```

---

### 4\. BOOLEAN → PHP Boolean

* **Database side**: `BOOLEAN` usually stores `0` (false) or `1` (true).
    
* **PHP side**: A **boolean** can be either `true` or `false`.
    

Example:

```php
<?php
$isLoggedIn = true;
if ($isLoggedIn) {
    echo "Welcome back!";
}
?>
```

---

### 5\. DATE / DATETIME → PHP Date & Time Functions

* **Database side**: `DATE`, `TIME`, `DATETIME` store calendar values.
    
* **PHP side**: PHP doesn’t have a specific **date variable type**, but it has functions and objects to handle dates.
    

Example:

```php
<?php
$currentDate = date("Y-m-d H:i:s");
echo $currentDate; // Output: 2025-09-04 17:35:00 (example)
?>
```

---

### 6\. NULL → PHP NULL

* **Database side**: `NULL` means "no value assigned."
    
* **PHP side**: `NULL` is a special type that represents an empty variable.
    

Example:

```php
<?php
$myVar = NULL;
var_dump($myVar); // Output: NULL
?>
```

---

## Type Juggling and Type Casting

One unique thing about PHP variables is **type juggling**. PHP automatically converts variables from one type to another when needed.

Example:

```php
<?php
$number = "100"; // This is a string
$sum = $number + 50; // PHP converts it to an integer
echo $sum; // Output: 150
?>
```

You can also **force type casting**:

```php
<?php
$val = "50.25";
$intVal = (int)$val;
echo $intVal; // Output: 50
?>
```

---

## Variable Scope in PHP

The **scope** of a variable defines where it can be accessed.

1. **Local Scope** → Inside a function
    
2. **Global Scope** → Outside functions
    
3. **Static Scope** → Retains value inside a function between calls
    
4. **Superglobals** → Special built-in variables (`$_POST`, `$_GET`, `$_SESSION`, etc.)
    

Example:

```php
<?php
$globalVar = "Hello"; // Global

function test() {
    $localVar = "World"; // Local
    echo $localVar;
}
test(); // Output: World
echo $globalVar; // Output: Hello
?>
```

---

## Conclusion

Variables in PHP are **flexible, dynamic, and powerful**. Unlike databases where you must define fixed data types (`VARCHAR`, `INT`, `BOOLEAN`), PHP handles this automatically.

* Strings in PHP work similarly to `VARCHAR`.
    
* Numbers map to `INT` or `FLOAT`.
    
* Booleans represent true/false logic.
    
* Dates require PHP functions or objects.
    
* NULL values work the same way as databases.
    

Understanding these concepts helps you write cleaner, more efficient PHP code — and also makes it easier to work with databases since you’ll know how PHP variables map to SQL data types.
