← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 10 of 13

PHP Variables

एक PHP variable information hold करने के लिए एक labeled box है, और इसका label हमेशा एक dollar sign से शुरू होता है। आप जब चाहें कुछ डाल सकते हैं, उसे देख सकते हैं, या उसे किसी नई चीज़ से बदल सकते हैं।
Syntax
php
$variable_name = value;

echo $variable_name;
echo "Text with $variable_name inside";

Variable Declaration

हर PHP variable name एक dollar sign से शुरू होता है — $name, name नहीं — यही तरीका है जिससे interpreter एक नज़र में एक variable को किसी constant, function, या keyword से अलग बताता है।

आप कभी पहले से एक type declare नहीं करते; variable बस पहली बार आपके इसे एक value assign करने पर exist करना शुरू करता है।

उदाहरण: Variable Declaration

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print `$name` to the output
echo $name;
?>

Dynamic Typing

क्योंकि PHP dynamically typed है, वही variable आपकी script में एक point पर एक string और बाद में एक number hold कर सकता है — PHP बस इसे reassign करके आगे बढ़ जाता है।

यह flexibility quick scripts के लिए convenient है, लेकिन यही वजह है कि बड़े codebases में type-related bugs silently घुस सकते हैं।

उदाहरण: Dynamic Typing

php
<?php
// Declare `$value`, set to "Hello"
$value = "Hello";
// Print a detailed dump (with types) of `$value`
var_dump($value);
// Declare `$value`, set to `42`
$value = 42;
// Print a detailed dump (with types) of `$value`
var_dump($value);
?>

Variables को Output करना

Double-quoted strings जहाँ भी $variable उनके अंदर दिखता है वहाँ actively उसकी current value substitute कर देती हैं — इसे interpolation कहते हैं।

Single-quoted strings उस step को पूरी तरह skip कर देती हैं और literal characters $variable print करती हैं, जो तब तेज़ है जब आपको genuinely substitution की ज़रूरत न हो।

उदाहरण: Outputting Variables

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print "Double-quoted: $name\n" to the output
echo "Double-quoted: $name\n";
// Print 'Single-quoted: $name' to the output
echo 'Single-quoted: $name';
?>

Variable Scope

किसी function के बाहर declare किया गया variable global scope रखता है और पूरी script में top-level code को visible है।

किसी function के अंदर declare किया गया variable default रूप से उस function तक local है — यह हर call पर fresh बनता है और function के return होते ही गायब हो जाता है, इसके बाहर के code को invisible।

उदाहरण: Variable Scope

php
<?php
// Declare `$globalVar`, set to "I am global"
$globalVar = "I am global";

// Define the function `showScope` with no parameters
function showScope() {
    // Declare `$localVar`, set to "I am local"
    $localVar = "I am local";
    // Print `$localVar . "\n"` to the output
    echo $localVar . "\n";
}
// Call `showScope()`
showScope();
// Print `$globalVar` to the output
echo $globalVar;
?>

Variable Variables

PHP एक double dollar sign इस्तेमाल करके 'variable variables' support करता है: $$name $name की *value* को एक दूसरे variable के नाम की तरह इस्तेमाल करता है।

यह एक rare, advanced feature है — कुछ dynamic scenarios में genuinely उपयोगी, लेकिन ऐसे तरीकों से overuse करना आसान है जो code को trace करना मुश्किल बना दे।

उदाहरण: Variable Variables

php
<?php
// Declare `$name`, set to "color"
$name = "color";
$$name = "blue";
// Print `$color` to the output
echo $color;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $ prefix भूल जाना, name = Ann; लिखना, जिसे PHP एक constant assignment मानता है और parse करने में fail हो जाता है।
  2. assign करने से पहले एक variable इस्तेमाल करना, जैसे echo $total;, जो एक 'Undefined variable' warning raise करता है और कुछ print नहीं करता।
  3. यह उम्मीद करना कि एक global variable किसी function के अंदर visible होगा, जबकि functions की अपनी scope होती है और इसे देखने के लिए global या एक parameter चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.