PHP Variables & Data Types क्या है?
Complete Guide in Hindi
PHP Variables की पूरी जानकारी हिंदी में — Variable Rules, Naming Conventions, 8 Data Types, Type Juggling, var_dump, और gettype। Real code examples के साथ।
📋 इस Article में क्या-क्या है (Table of Contents)
- Variables क्या होते हैं?
- Variable declare करना
- Variable Naming Rules
- Variable Scope
- PHP Data Types — Overview
- Scalar Types (4 types)
- Compound Types (2 types)
- Special Types (2 types)
- Type Juggling & Type Casting
- Quick Reference + FAQ
Variable एक container है जो data store करता है। जैसे एक डिब्बे में कुछ रख सकते हो और बाद में निकाल सकते हो — वैसे ही variable में value store होती है जिसे program में कहीं भी use कर सकते हैं।
// Integer (पूर्णांक)
$umar = 25;
$marks = 95;
// Float (दशमलव)
$price = 99.50;
$height = 5.11;
// String (text)
$naam = "Rahul Kumar";
$city = "Delhi";
// Boolean (true/false)
$isLogin = true;
$isAdmin = false;
// NULL (कोई value नहीं)
$address = null;
// Use करना
echo "नाम: " . $naam;
echo "उम्र: $umar साल";
?>
$x = 10; // अभी integer है
echo $x; // Output: 10
$x = "Hello"; // अब string हो गया — PHP allow करता है
echo $x; // Output: Hello
$x = 3.14; // अब float हो गया
echo $x; // Output: 3.14
?>
$ sign से शुरू होना ज़रूरी है
$naam, $age, $userEmail — हमेशा $ से शुरू। बिना $ के variable नहीं होगा।
Letter या Underscore से शुरू हो ($ के बाद)
$naam ✅ $_temp ✅ $1naam ❌ — number से शुरू नहीं हो सकता।
Letters, Numbers, Underscore use कर सकते हैं
$user_name ✅ $age25 ✅ $user-name ❌ — hyphen नहीं चलेगा।
Case Sensitive है
$naam, $Naam, $NAAM — तीनों अलग-अलग variables हैं। Consistent रहो।
Reserved Words use नहीं कर सकते
$if, $echo, $class — PHP keywords को variable name नहीं बना सकते।
✅ अच्छे Variable Names
$user_email // snake_case
$totalPrice // meaningful
$isLoggedIn // boolean clear
$productCount // descriptive
❌ बुरे Variable Names
$1user // number से शुरू
$my-var // hyphen invalid
$a // too short
$DATA // ALL CAPS avoid
Global Scope
Function के बाहर declare किया variable। पूरी file में accessible — लेकिन functions के अंदर directly नहीं।
Local Scope
Function के अंदर declare किया variable। सिर्फ उसी function में accessible। बाहर से नहीं।
Static Scope
Function call के बाद भी value याद रहती है। static keyword से declare होता है।
$globalVar = "Main global हूँ";
function myFunction() {
// $globalVar यहाँ directly accessible नहीं!
// global keyword से access करना होगा:
global $globalVar;
echo $globalVar; // अब चलेगा
$localVar = "Main local हूँ"; // सिर्फ यहाँ
}
function counter() {
static $count = 0; // static — value याद रहती है
$count++;
echo $count . " ";
}
counter(); // 1
counter(); // 2
counter(); // 3 — value याद रही!
?>
Data Type बताता है कि variable में किस तरह का data है। PHP में 8 Data Types होते हैं जिन्हें 3 categories में बाँटा गया है: Scalar (4), Compound (2), और Special (2)।
| Category | Data Type | Example | क्या store होता है? |
|---|---|---|---|
| Scalar | Integer | $x = 10; | पूर्णांक numbers |
| Scalar | Float | $x = 3.14; | दशमलव numbers |
| Scalar | String | $x = "Hello"; | Text / Characters |
| Scalar | Boolean | $x = true; | true या false |
| Compound | Array | $x = [1,2,3]; | Multiple values |
| Compound | Object | $x = new Car(); | Class का instance |
| Special | NULL | $x = null; | कोई value नहीं |
| Special | Resource | $x = fopen(...); | External resource |
Integer (पूर्णांक)
Whole numbers — positive, negative, zero
Decimal (10), Octal (0123), Hexadecimal (0x1A), Binary (0b1010) — सभी forms support होते हैं।
$age = 25; $temp = -5;Float / Double (दशमलव)
Decimal numbers — scientific notation भी
Financial calculations, measurements, percentages के लिए। Floating point precision issues से सावधान रहें।
$price = 99.99; $pi = 3.14159;String (टेक्स्ट)
Characters का sequence — quotes में
Single या double quotes में। Double quotes में variables expand होते हैं। Heredoc और Nowdoc syntax भी available।
$name = "Rahul"; $msg = 'Hello';Boolean (सच/झूठ)
सिर्फ दो values — true या false
Conditions, flags, login status के लिए। false, 0, "", "0", [], null — सब false माने जाते हैं।
$isLogin = true; $isDone = false;// INTEGER
$positive = 42;
$negative = -15;
$hexNum = 0x1A; // Hex = 26
$binNum = 0b1010; // Binary = 10
// FLOAT
$price = 1299.99;
$sci = 1.5e3; // = 1500.0
// STRING
$single = 'Rahul $naam'; // variable expand नहीं होगा
$double = "Hello $single"; // variable expand होगा
$multiline = "Line 1\nLine 2\nLine 3"; // \n = newline
// BOOLEAN
$isTrue = true;
$isFalse = false;
if ($isTrue) {
echo "Login successful!";
}
?>
Array (सूची)
Multiple values एक variable में
Indexed array, Associative array, Multidimensional array। Products list, user data, settings — सब array में।
$fruits = ["Apple", "Mango", "Banana"];Object (वस्तु)
Class का instance — OOP का core
Properties और methods वाला complex data type। new keyword से create होता है।
$car = new Car("Toyota");// 1. Indexed Array — number से index
$fruits = ["Apple", "Mango", "Banana"];
echo $fruits[0]; // Apple
echo $fruits[1]; // Mango
// 2. Associative Array — key-value pair
$user = [
"naam" => "Rahul",
"umar" => 25,
"city" => "Delhi"
];
echo $user["naam"]; // Rahul
// 3. Multidimensional Array
$students = [
["naam" => "Priya", "marks" => 95],
["naam" => "Amit", "marks" => 88],
];
echo $students[0]["naam"]; // Priya
?>
NULL
कोई value नहीं — empty
Variable को explicitly null assign करो, या unset() करो, या declare करो बिना value दिए — सब NULL होता है।
$x = null; unset($y);Resource
External resource का reference
Database connection, file handle, image — ये सब resources हैं। Functions इन्हें create और return करते हैं।
$file = fopen("file.txt", "r");// NULL
$x = null;
var_dump($x); // NULL
echo is_null($x) ? "हाँ NULL है" : "NULL नहीं";
// unset करने पर variable NULL हो जाता है
$naam = "Rahul";
unset($naam); // अब $naam exist नहीं करता
// RESOURCE — file open करना
$file = fopen("test.txt", "r");
echo gettype($file); // resource
fclose($file); // use के बाद close करो
?>
// PHP automatically string को number बना देता है
$a = "10"; // string
$b = 5; // integer
$sum = $a + $b; // PHP "10" को 10 बना देता है
echo $sum; // 15
// String concatenation vs Addition
echo "5" + 3; // 8 (arithmetic)
echo "5" . 3; // 53 (concatenation)
// var_dump — type और value दोनों दिखाता है
var_dump(42); // int(42)
var_dump(3.14); // float(3.14)
var_dump("Hello"); // string(5) "Hello"
var_dump(true); // bool(true)
var_dump(null); // NULL
?>
$str = "42.5 rupees";
// (int) — integer में convert
echo (int) $str; // 42
// (float) — float में convert
echo (float) $str; // 42.5
// (string) — string में convert
$num = 100;
echo (string) $num; // "100"
// (bool) — boolean में convert
echo (bool) 0; // false
echo (bool) 1; // true
echo (bool) ""; // false
echo (bool) "Hello"; // true
// (array) — array में convert
$name = "Rahul";
print_r((array) $name); // Array([0] => Rahul)
?>
| Casting Syntax | Function Alternative | क्या करता है? |
|---|---|---|
| (int) $x | intval($x) | Integer में convert |
| (float) $x | floatval($x) | Float में convert |
| (string) $x | strval($x) | String में convert |
| (bool) $x | boolval($x) | Boolean में convert |
| (array) $x | — | Array में convert |
| gettype($x) | — | Type का नाम return करता है |
| var_dump($x) | — | Type + Value दोनों दिखाता है |
| is_int($x) | is_string, is_float… | Type check करता है |
| Concept | Syntax | Note |
|---|---|---|
| Declare | $naam = "Rahul"; | $ से शुरू, type declare नहीं |
| Integer | $x = 42; | Whole number |
| Float | $x = 3.14; | Decimal number |
| String | $x = "Hello"; | Double/single quotes |
| Boolean | $x = true; | true / false only |
| Array | $x = [1, 2, 3]; | Multiple values |
| NULL | $x = null; | Empty value |
| Type Check | gettype($x) | Type name return |
| Debug | var_dump($x) | Type + value दोनों |
| Global Scope | global $varName; | Function में global use |
| Static | static $count = 0; | Value याद रहती है |
| Unset | unset($x); | Variable delete |
PHP Variables और Data Types — यह वो building blocks हैं जिनके बिना कोई PHP program नहीं चल सकता। Variables data store करते हैं, Data Types define करते हैं कि वो data किस तरह का है।
Variables $ से शुरू होते हैं — case sensitive और loosely typed।
8 Data Types हैं — Integer, Float, String, Boolean, Array, Object, NULL, Resource।
PHP Type Juggling करता है — automatically types convert होते हैं।
var_dump() debugging का best tool है — type और value दोनों दिखाता है।
Meaningful variable names लिखो — code readable और maintainable बनता है।
=== use करो == की जगह — type juggling bugs से बचाता है।