PHP Variables
In this page:
$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
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print `$name` to the output
echo $name;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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';
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// Declare `$name`, set to "color"
$name = "color";
$$name = "blue";
// Print `$color` to the output
echo $color;
?>
Login to try C/C++/Java/PHP code in the editor
$prefix भूल जाना,name = Ann;लिखना, जिसे PHP एक constant assignment मानता है और parse करने में fail हो जाता है।- assign करने से पहले एक variable इस्तेमाल करना, जैसे
echo $total;, जो एक 'Undefined variable' warning raise करता है और कुछ print नहीं करता। - यह उम्मीद करना कि एक global variable किसी function के अंदर visible होगा, जबकि functions की अपनी scope होती है और इसे देखने के लिए
globalया एक parameter चाहिए।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: