variable किसी memory को दिया गया नाम है जो कि किसी value को Hold करती है या store करती है। मतलब जब हम Variable define कर रहें है तो हम memory में value को store करके उसे नाम दे रहे होते हैं।

Operator Operands

PHP में Variables $ sign के साथ define किये जाते हैं , Valid PHP Variables को letter और underscore के साथ define किया जा सकता है।

Copy Fullscreen Close Fullscreen
<?php
  $_4site = 'Valid Variable';     
  // valid; starts with an underscore  
  echo $_4site."<br>";  
  $4site = 'Invalid variable';       
  // invalid; starts with a number  
  echo $4site;
 ?>
Output
Parse error: syntax error, unexpected '4' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\test\variables.php on line 5

अगर आप किसी variable को number के साथ start करते हैं तो Error आएगी।

PHP Variables case sensitive होते हैं , this means अगर आप एक ही name के Variables को small और capital letters में define करते हाँ तो दोनों ही Variables different होंगे , और different value hold करेंगे।

Copy<?php
$var = 'rahul';
$Var = 'Rahul';
echo "$var, $Var";
?>
Output
rahul, Rahul

ऊपर दिए गए example में आप देख सकते हैं , कि same name के define किये गए हैं लेकिन दोनों ही variables different value Hold किये हुए हैं।

Note - $this special variable है आप इस variable को कुछ भी assign नहीं कर सकते है , हालांकि तह कहाँ पर use होता है ये हम आगे सीखेंगे।

PHP Variables define करते समय हमें कोई type करने की जरूरत भी नहीं पड़ती है , जैसे कि JAVA या C में कोई variable define करते समय इसका टाइप define करना पड़ता है। आप जिस type की value को assign कराओगे variable उसी type की value को hold कर लेगा।

Example -

Copy Fullscreen Close Fullscreen
<?php
$a = "String";
$b = 'String';
$c = 0;
$d = 2.5;
$e = 'C';
$f = true;
$g = '';
$h = null;
echo var_dump($a)."<br>";
echo var_dump($b)."<br>";
echo var_dump($c)."<br>";
echo var_dump($d)."<br>";
echo var_dump($e)."<br>";
echo var_dump($f)."<br>";
echo var_dump($g)."<br>";
echo var_dump($h)."<br>";
?>
Output
string(6) "String"
string(6) "String"
int(0)
float(2.5)
string(1) "C"
bool(true)
string(0) ""
NULL

इसके अलावा PHP हमें कुछ predefined variables भी provide करती है जिन्हे Super Global Variables कहते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers