PHP Basics · Beginner Level · Chapter 4

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 के साथ।

📦 Variables 🔢 8 Data Types 🔄 Type Juggling ⏱️ 10 min read 📝 1800+ words
$Variable हमेशा $ से शुरू
8PHP Data Types
NULLEmpty value का type
looselyPHP Typed Language है

📋 इस Article में क्या-क्या है (Table of Contents)

  1. Variables क्या होते हैं?
  2. Variable declare करना
  3. Variable Naming Rules
  4. Variable Scope
  5. PHP Data Types — Overview
  6. Scalar Types (4 types)
  7. Compound Types (2 types)
  8. Special Types (2 types)
  9. Type Juggling & Type Casting
  10. Quick Reference + FAQ
1
Variables क्या होते हैं?

Variable एक container है जो data store करता है। जैसे एक डिब्बे में कुछ रख सकते हो और बाद में निकाल सकते हो — वैसे ही variable में value store होती है जिसे program में कहीं भी use कर सकते हैं।

PHP में variable हमेशा dollar sign $ से शुरू होता है। PHP एक loosely typed language है — इसका मतलब है कि variable declare करते समय data type बताना ज़रूरी नहीं। PHP खुद automatically type decide कर लेता है।
यह C या Java से अलग है जहाँ int x = 5; लिखना पड़ता है। PHP में बस $x = 5; काफी है — PHP समझ जाता है कि यह integer है।
Variable = नाम वाला डिब्बा जिसमें कोई भी value रख सकते हो — number, text, list, कुछ भी।

2
Variable Declare और Assign करना
PHP में variable declare करना बहुत simple है — बस $naam = value; लिखो। Variable automatically create हो जाता है जब आप उसे पहली बार value assign करते हैं।
VARIABLE DECLARE करना — Basic Examples
<?php

// 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 साल";
?>
PHP में एक variable की value बाद में बदल भी सकते हो — और type भी बदल सकता है:
VALUE और TYPE दोनों बदल सकते हैं
<?php
$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
?>
Loosely Typed Language: PHP automatically type manage करता है। यह convenience है लेकिन sometimes unexpected bugs भी ला सकता है — इसीलिए PHP 7+ में Type Declarations आए।

3
Variable Naming Rules & Conventions
PHP variables के नाम रखने के कुछ strict rules हैं — इन्हें follow न करने पर error आएगी। और कुछ conventions हैं — जो follow करना best practice है।

$ 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

$userName // camelCase
$user_email // snake_case
$totalPrice // meaningful
$isLoggedIn // boolean clear
$productCount // descriptive

❌ बुरे Variable Names

$x // meaningless
$1user // number से शुरू
$my-var // hyphen invalid
$a // too short
$DATA // ALL CAPS avoid
💡 Convention: PHP community में camelCase ($userName) या snake_case ($user_name) दोनों popular हैं। Laravel में camelCase prefer होता है। जो भी choose करो — पूरे project में consistent रहो।

4
Variable Scope — कहाँ available है?
Scope का मतलब है — variable program में कहाँ-कहाँ accessible है। PHP में तीन types के scope होते हैं।
Global
Global Scope

Function के बाहर declare किया variable। पूरी file में accessible — लेकिन functions के अंदर directly नहीं।

Local
Local Scope

Function के अंदर declare किया variable। सिर्फ उसी function में accessible। बाहर से नहीं।

Static
Static Scope

Function call के बाद भी value याद रहती है। static keyword से declare होता है।

SCOPE — Examples
<?php
$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 याद रही!
?>
Common Mistake: Global variable को function के अंदर directly use करने की कोशिश — यह PHP में काम नहीं करता। global $varName; लिखना ज़रूरी है।

5
PHP Data Types — Overview

Data Type बताता है कि variable में किस तरह का data है। PHP में 8 Data Types होते हैं जिन्हें 3 categories में बाँटा गया है: Scalar (4), Compound (2), और Special (2)

CategoryData TypeExampleक्या store होता है?
ScalarInteger$x = 10;पूर्णांक numbers
ScalarFloat$x = 3.14;दशमलव numbers
ScalarString$x = "Hello";Text / Characters
ScalarBoolean$x = true;true या false
CompoundArray$x = [1,2,3];Multiple values
CompoundObject$x = new Car();Class का instance
SpecialNULL$x = null;कोई value नहीं
SpecialResource$x = fopen(...);External resource

6
Scalar Types — 4 Basic Data Types
Scalar types वो हैं जिनमें एक single value store होती है। PHP के सबसे ज़्यादा use होने वाले types।
int
Integer (पूर्णांक)

Whole numbers — positive, negative, zero

Decimal (10), Octal (0123), Hexadecimal (0x1A), Binary (0b1010) — सभी forms support होते हैं।

$age = 25; $temp = -5;
flt
Float / Double (दशमलव)

Decimal numbers — scientific notation भी

Financial calculations, measurements, percentages के लिए। Floating point precision issues से सावधान रहें।

$price = 99.99; $pi = 3.14159;
str
String (टेक्स्ट)

Characters का sequence — quotes में

Single या double quotes में। Double quotes में variables expand होते हैं। Heredoc और Nowdoc syntax भी available।

$name = "Rahul"; $msg = 'Hello';
bool
Boolean (सच/झूठ)

सिर्फ दो values — true या false

Conditions, flags, login status के लिए। false, 0, "", "0", [], null — सब false माने जाते हैं।

$isLogin = true; $isDone = false;
SCALAR TYPES — Detailed Examples
<?php

// 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!";
}
?>

7
Compound Types — Array और Object
Compound types में multiple values store हो सकती हैं। यह real-world applications में सबसे ज़्यादा use होते हैं।
arr
Array (सूची)

Multiple values एक variable में

Indexed array, Associative array, Multidimensional array। Products list, user data, settings — सब array में।

$fruits = ["Apple", "Mango", "Banana"];
obj
Object (वस्तु)

Class का instance — OOP का core

Properties और methods वाला complex data type। new keyword से create होता है।

$car = new Car("Toyota");
ARRAY — तीनों Types
<?php

// 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
?>

8
Special Types — NULL और Resource
null
NULL

कोई value नहीं — empty

Variable को explicitly null assign करो, या unset() करो, या declare करो बिना value दिए — सब NULL होता है।

$x = null;   unset($y);
res
Resource

External resource का reference

Database connection, file handle, image — ये सब resources हैं। Functions इन्हें create और return करते हैं।

$file = fopen("file.txt", "r");
NULL और RESOURCE — Examples
<?php

// 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 करो
?>

9
Type Juggling & Type Casting
Type Juggling — PHP automatically type convert करता है जब ज़रूरत हो। Type Casting — आप manually type convert करते हो।
TYPE JUGGLING — PHP automatic conversion
<?php

// 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
?>
TYPE CASTING — Manual Conversion
<?php

$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 SyntaxFunction Alternativeक्या करता है?
(int) $xintval($x)Integer में convert
(float) $xfloatval($x)Float में convert
(string) $xstrval($x)String में convert
(bool) $xboolval($x)Boolean में convert
(array) $xArray में convert
gettype($x)Type का नाम return करता है
var_dump($x)Type + Value दोनों दिखाता है
is_int($x)is_string, is_float…Type check करता है
💡 var_dump() vs print_r() vs echo: Debugging के लिए var_dump() सबसे useful है — type और value दोनों दिखाता है। print_r() arrays के लिए readable output देता है। echo सिर्फ value print करता है।

10
Quick Reference — Variables एक नज़र में
ConceptSyntaxNote
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 Checkgettype($x)Type name return
Debugvar_dump($x)Type + value दोनों
Global Scopeglobal $varName;Function में global use
Staticstatic $count = 0;Value याद रहती है
Unsetunset($x);Variable delete

?
अक्सर पूछे जाने वाले सवाल (FAQ)
Q: PHP में variable declare करने से पहले type बताना ज़रूरी है?
नहीं। PHP loosely typed language है — बस $naam = "Rahul"; लिखो, PHP खुद string type assign कर देता है। लेकिन PHP 7+ में functions में type declare कर सकते हो: function add(int $a, int $b): int { ... } — यह strict typing के लिए है।
Q: NULL और empty string ("") में क्या फर्क है?
NULL का मतलब है variable में कोई value ही नहीं है — यह "nothing" है। Empty string ("") एक valid string value है जिसमें zero characters हैं। var_dump(null) → NULL देता है, var_dump("") → string(0) "" देता है। is_null("") → false और is_null(null) → true।
Q: Type Juggling से क्या problems आ सकती हैं?
PHP में == (loose comparison) type juggling करता है — "0" == false → true हो जाता है जो unexpected है। इससे बचने के लिए हमेशा === (strict comparison) use करो जो type और value दोनों check करता है। यह PHP का सबसे common bug source है।
Q: $GLOBALS क्या है?
$GLOBALS एक PHP superglobal array है जिसमें सभी global variables accessible हैं। Functions के अंदर global keyword की जगह $GLOBALS['varName'] use कर सकते हो। उदाहरण: $GLOBALS['counter']++; — लेकिन यह modern PHP में avoid किया जाता है।
Q: Variable variables क्या होते हैं?
PHP में variable का नाम भी variable हो सकता है — इसे variable variable कहते हैं। $varName = "naam"; $$varName = "Rahul"; // यह $naam = "Rahul" जैसा है। echo $$varName; → Rahul। यह advanced PHP feature है, beginners को पहले basics clear करने चाहिए।
Q: PHP में integer की maximum value कितनी है?
यह system पर depend करता है। 64-bit system पर PHP_INT_MAX = 9223372036854775807 (2^63 - 1)। इससे बड़े numbers के लिए float use होता है (precision कम होती है) या BCMath/GMP extensions use करते हैं।

निष्कर्ष (Conclusion)

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 से बचाता है।

🚀 अगला कदम: Variables और Data Types clear हो गए? अब अगले chapter में PHP Constants सीखें — वो values जो एक बार set होने के बाद बदलती नहीं। define() और const में क्या फर्क है — सब detail में।