← Back to PHP Course | Chapter 3: Operators | Lesson 5 of 8

PHP Assignment Operators

Assignment operators किसी variable में एक value डालते हैं, किसी piggy bank में coin डालने जैसे। किसी में add करने जैसे shortcuts आपको खुद को दोहराए बिना 'और 5 add करो' कहने देते हैं।
Syntax
php
$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
<?php
// Declare `$total`, set to `5`
$total = 5;
// Print `$total` to the output
echo $total;
?>

Add और Subtract Assignment

+= और -= एक operation को एक assignment के साथ एक step में combine करते हैं: $total += 5 exactly $total = $total + 5 के बराबर है, बस लिखने में छोटा और, कई लोगों के लिए, pattern familiar होते ही पढ़ना आसान।

उदाहरण: Add and Subtract Assignment

php
<?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;
?>

Multiply और Divide Assignment

*= और /= multiplication और division के लिए identical pattern follow करते हैं — $price *= 1.1 $price पर एक 10% increase apply करता है और result को एक ही statement में वापस उसी variable में store कर देता है।

उदाहरण: Multiply and Divide Assignment

php
<?php
// Declare `$price`, set to `100`
$price = 100;
$price *= 1.1;
// Print `$price` to the output
echo $price;
?>

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
<?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;
?>

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
<?php
// Declare `$username`, set to `null`
$username = null;
$username ??= "Guest";
// Print `$username` to the output
echo $username;
?>
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. += के बजाय =+ लिखना, जो variable में add करने के बजाय एक positive number assign करता है।
  2. एक undefined variable पर .= इस्तेमाल करना, जो एक undefined variable warning raise करता है; पहले इसे $s = ''; से initialize करें।
  3. strings जोड़ने के लिए += इस्तेमाल करना, जबकि यह numeric addition करता है; concatenation के लिए .= इस्तेमाल करें।

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.