PHP Assignment Operators
In this page:
$variable = value;
$variable += value; // $variable = $variable + value
$variable -= value;
$variable *= value;
$variable /= value;
Basic Assignment (=)
= right पर मौजूद value को left पर मौजूद variable में store करता है — PHP में सबसे basic operation, और वह जिसका नीचे दिया हर दूसरा assignment operator एक shorthand है।
उदाहरण: Basic Assignment (=)
<?php
// Declare `$total`, set to `5`
$total = 5;
// Print `$total` to the output
echo $total;
?>
Login to try C/C++/Java/PHP code in the editor
Add और Subtract Assignment
+= और -= एक operation को एक assignment के साथ एक step में combine करते हैं: $total += 5 exactly $total = $total + 5 के बराबर है, बस लिखने में छोटा और, कई लोगों के लिए, pattern familiar होते ही पढ़ना आसान।
उदाहरण: Add and Subtract Assignment
<?php
// Declare `$total`, set to `10`
$total = 10;
$total += 5;
// Print `$total . "\n"` to the output
echo $total . "\n";
$total -= 3;
// Print `$total` to the output
echo $total;
?>
Login to try C/C++/Java/PHP code in the editor
Multiply और Divide Assignment
*= और /= multiplication और division के लिए identical pattern follow करते हैं — $price *= 1.1 $price पर एक 10% increase apply करता है और result को एक ही statement में वापस उसी variable में store कर देता है।
उदाहरण: Multiply and Divide Assignment
<?php
// Declare `$price`, set to `100`
$price = 100;
$price *= 1.1;
// Print `$price` to the output
echo $price;
?>
Login to try C/C++/Java/PHP code in the editor
Modulo और Concat Assignment
%= किसी variable को उसे किसी दूसरी value से divide करने के remainder में reassign करता है, और .= किसी मौजूदा string variable के अंत में एक string append करता है — numbers के लिए += का string-concatenation equivalent, और piece by piece HTML output बनाने वाले code में सबसे common operators में से एक।
उदाहरण: Modulo and Concat Assignment
<?php
// Declare `$n`, set to `10`
$n = 10;
$n %= 3;
// Print `$n . "\n"` to the output
echo $n . "\n";
// Declare `$html`, set to "<p>"
$html = "<p>";
$html .= "Hello";
$html .= "</p>";
// Print `$html` to the output
echo $html;
?>
Login to try C/C++/Java/PHP code in the editor
Null Coalescing Assignment (??=)
??= सिर्फ तभी right-hand value assign करता है जब left-hand variable इस समय null हो या अभी exist ही न करे — यह पहले से set variable को untouched छोड़ देता है।
यह इसे बिना पहले explicit isset() check लिखे एक default value भरने का concise तरीका बनाता है।
उदाहरण: Null Coalescing Assignment (??=)
<?php
// Declare `$username`, set to `null`
$username = null;
$username ??= "Guest";
// Print `$username` to the output
echo $username;
?>
Login to try C/C++/Java/PHP code in the editor
+=के बजाय=+लिखना, जो variable में add करने के बजाय एक positive number assign करता है।- एक undefined variable पर
.=इस्तेमाल करना, जो एक undefined variable warning raise करता है; पहले इसे$s = '';से initialize करें। - strings जोड़ने के लिए
+=इस्तेमाल करना, जबकि यह numeric addition करता है; concatenation के लिए.=इस्तेमाल करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: