Learn PHP Basics

Search for a command to run...

No comments yet. Be the first to comment.
PHP (Hypertext Preprocessor) is one of the most widely used server-side scripting languages for web development. Like all programming languages, PHP has a defined grammar and syntax that dictate how code should be structured. In this blog, we will ex...
Discover interesting JavaScript concepts, essential fundamentals, and common interview questions—all in one place.
How to kick start a Laravel project? First we need to go Laravel official website and open getting started page. If you’re creating this application for the first time you need to install PHP, Composer and Laravel installer. In addition, you should i...

PHP (Hypertext Preprocessor) is one of the most widely used server-side scripting languages for web development. Like all programming languages, PHP has a defined grammar and syntax that dictate how code should be structured. In this blog, we will ex...
PHP (Hypertext Preprocessor) has been a dominant server-side scripting language for web development since its inception. With the release of PHP 8, developers gained access to powerful new features, optimizations, and improvements that enhance both p...
On this page
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:
:: and the method/property name. class MyClass {
public static $staticProperty = "Hello";
public static function staticMethod() {
return "World";
}
}
echo MyClass::$staticProperty; // Output: Hello
echo MyClass::staticMethod(); // Output: World
:: and the constant name. class MyClass {
const MY_CONSTANT = "PHP";
}
echo MyClass::MY_CONSTANT; // Output: PHP
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. 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!
self): Inside a class, self:: can be used to refer to static properties, static methods, or constants defined within that same class. 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.
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
$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)
A variable must start with a $ sign.
The first character after $ must be a letter or an underscore (_).
Variable names are case-sensitive ($Name and $name are different).
✅ Valid:
$myVar = 100;
$_value = "Hello";
$Name = "PHP";
❌ Invalid:
$1name = "Wrong"; // cannot start with a number
$my-var = 20; // cannot use a hyphen
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.
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
$greeting = "Hello, World!";
echo $greeting; // Output: Hello, World!
?>
🔹 PHP manages memory dynamically for strings, just like how VARCHAR works in databases.
Database side: INT stores whole numbers.
PHP side: An integer is a whole number, positive or negative, without decimals.
Example:
<?php
$age = 30;
$year = -2025;
echo $age + 5; // Output: 35
?>
Database side: DECIMAL or FLOAT is used for numbers with decimal points.
PHP side: A float stores numbers with fractions or decimals.
Example:
<?php
$price = 99.99;
$pi = 3.14159;
echo $pi * 2; // Output: 6.28318
?>
Database side: BOOLEAN usually stores 0 (false) or 1 (true).
PHP side: A boolean can be either true or false.
Example:
<?php
$isLoggedIn = true;
if ($isLoggedIn) {
echo "Welcome back!";
}
?>
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
$currentDate = date("Y-m-d H:i:s");
echo $currentDate; // Output: 2025-09-04 17:35:00 (example)
?>
Database side: NULL means "no value assigned."
PHP side: NULL is a special type that represents an empty variable.
Example:
<?php
$myVar = NULL;
var_dump($myVar); // Output: NULL
?>
One unique thing about PHP variables is type juggling. PHP automatically converts variables from one type to another when needed.
Example:
<?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
$val = "50.25";
$intVal = (int)$val;
echo $intVal; // Output: 50
?>
The scope of a variable defines where it can be accessed.
Local Scope → Inside a function
Global Scope → Outside functions
Static Scope → Retains value inside a function between calls
Superglobals → Special built-in variables ($_POST, $_GET, $_SESSION, etc.)
Example:
<?php
$globalVar = "Hello"; // Global
function test() {
$localVar = "World"; // Local
echo $localVar;
}
test(); // Output: World
echo $globalVar; // Output: Hello
?>
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.