PHP Variables
In PHP, variables are used to store data like strings, numbers, arrays, and more. All variables start with a $
sign followed by the variable name.
Rules to Declare PHP Variables:
- Variable names start with
$
(dollar sign) - Must begin with a letter or underscore (_)
- Cannot start with a number
- Can contain only letters, numbers, and underscores
- Variable names are case-sensitive (
$name
and$Name
are different)
PHP में वेरिएबल्स का उपयोग डेटा को स्टोर करने के लिए किया जाता है जैसे कि टेक्स्ट, नंबर आदि। सभी वेरिएबल्स की शुरुआत $
से होती है।
PHP वेरिएबल डिक्लेयर करने के नियम:
- वेरिएबल नाम
$
से शुरू होता है - यह अक्षर या अंडरस्कोर (_) से शुरू होना चाहिए
- संख्या से शुरू नहीं हो सकता
- केवल अक्षर, संख्याएं और अंडरस्कोर शामिल हो सकते हैं
- वेरिएबल नाम केस सेंसिटिव होते हैं (
$name
और$Name
अलग हैं)
Example 1: Basic Variable
<?php
$name = "John";
$age = 25;
echo "My name is $name and I am $age years old.";
?>
Example 2: Case Sensitivity
<?php
$x = 10;
echo $x; // Output: 10
echo $X; // Error: Undefined variable
?>
Example 3: Variable with Underscore
<?php
$user_name = "livecoder";
echo $user_name;
?>
Example 4: Reassigning Values
<?php
$color = "Red";
echo $color;
$color = "Blue"; // Reassigning
echo $color;
?>