{"id":441,"date":"2021-03-15T15:38:37","date_gmt":"2021-03-15T15:38:37","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=441"},"modified":"2025-04-09T08:53:31","modified_gmt":"2025-04-09T08:53:31","slug":"php-assignment-operators","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-assignment-operators\/","title":{"rendered":"PHP Assignment Operators"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the most commonly used PHP assignment operators.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-assignment-operator'>Introduction to the PHP assignment operator <a href=\"#introduction-to-the-php-assignment-operator\" class=\"anchor\" id=\"introduction-to-the-php-assignment-operator\" title=\"Anchor for Introduction to the PHP assignment operator\">#<\/a><\/h2>\n\n\n\n<p>PHP uses the <code>=<\/code> to represent the assignment operator. <\/p>\n\n\n\n<p>The following shows how to assign a value to a variable using the assignment operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$variable_name = expression;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>On the left side of the assignment operator (<code>=<\/code>) is a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-variables\/\">variable<\/a> to which you want to assign a value. And on the right side of the assignment operator (<code>=<\/code>) is a value or an expression.<\/p>\n\n\n\n<p>When evaluating the assignment operator (<code>=<\/code>), PHP evaluates the expression on the right side first and assigns the result to the variable on the left side. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$x = <span class=\"hljs-number\">10<\/span>;\n$y = <span class=\"hljs-number\">20<\/span>;\n$total = $x + $y;\n\n<span class=\"hljs-keyword\">echo<\/span> $total;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiR4ID0gMTA7CiR5ID0gMjA7CiR0b3RhbCA9ICR4ICsgJHk7CgplY2hvICR0b3RhbDs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we assign 10 to <code>$x<\/code>, 20 to <code>$y<\/code>, and the sum of <code>$x<\/code> and <code>$y<\/code> to <code>$total<\/code>.<\/p>\n\n\n\n<p>The assignment expression returns a value assigned, which is the result of the expression:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$variable_name = expression;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This allows you to chain assignments in a single statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$x = $y = <span class=\"hljs-number\">20<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, PHP evaluates the right-most expression first:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$y = <span class=\"hljs-number\">20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The whole expression evaluates to <code>20<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> $y = <span class=\"hljs-number\">20<\/span> . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> $y . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmVjaG8gJHkgPSAyMCAuICc8YnI-JzsKZWNobyAkeSAuICc8YnI-Jzs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">20\n20\n<\/code><\/span><\/pre>\n\n\n<p>The assignment expression <code>$y = 20<\/code> returns <code>20<\/code> so PHP assigns <code>20<\/code> to <code>$x<\/code>. After the assignments, both <code>$x<\/code> and <code>$y<\/code> equal 20.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='arithmetic-assignment-operators'>Arithmetic assignment operators <a href=\"#arithmetic-assignment-operators\" class=\"anchor\" id=\"arithmetic-assignment-operators\" title=\"Anchor for Arithmetic assignment operators\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to increase a variable by a specific value. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$counter = <span class=\"hljs-number\">1<\/span>;\n$counter = $counter + <span class=\"hljs-number\">1<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span>  $counter;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjb3VudGVyID0gMTsKJGNvdW50ZXIgPSAkY291bnRlciArIDE7CgplY2hvICAkY291bnRlcjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, <code>$counter<\/code> is set to <code>1<\/code>.<\/li>\n\n\n\n<li>Then, increase the <code>$counter<\/code> by <code>1<\/code> and assign the result to the <code>$counter<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>After the assignments, the value of <code>$counter<\/code> is <code>2<\/code>.<\/p>\n\n\n\n<p>PHP provides the arithmetic assignment operator <code>+=<\/code> that can do the same but with a shorter code. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$counter = <span class=\"hljs-number\">1<\/span>;\n$counter += <span class=\"hljs-number\">1<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span>  $counter;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjb3VudGVyID0gMTsKJGNvdW50ZXIgKz0gMTsKCmVjaG8gICRjb3VudGVyOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The expression <code>$counter += 1<\/code> is equivalent to the expression <code>$counter = $counter + 1<\/code>.<\/p>\n\n\n\n<p>Besides the <code>+=<\/code> operator, PHP provides other arithmetic assignment operators. <\/p>\n\n\n\n<p>The following table shows all the arithmetic assignment operators in PHP:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Operator<\/th><th>Example<\/th><th>Equivalent<\/th><th>Operation<\/th><\/tr><\/thead><tbody><tr><td>+=<\/td><td>$x += $y<\/td><td>$x = $x + $y<\/td><td>Addition<\/td><\/tr><tr><td>-=<\/td><td>$x -= $y<\/td><td>$x = $x &#8211; $y<\/td><td>Subtraction<\/td><\/tr><tr><td>*=<\/td><td>$x *= $y<\/td><td>$x = $x * $y<\/td><td>Multiplication<\/td><\/tr><tr><td>\/=<\/td><td>$x \/= $y<\/td><td>$x = $x \/ $y<\/td><td>Division<\/td><\/tr><tr><td>%=<\/td><td>$x %= $y<\/td><td>$x = $x % $y<\/td><td>Modulus<\/td><\/tr><tr><td>**=<\/td><td>$x **= $y<\/td><td>$x = $x ** $y<\/td><td>Exponentiation<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='concatenation-assignment-operator'>Concatenation assignment operator <a href=\"#concatenation-assignment-operator\" class=\"anchor\" id=\"concatenation-assignment-operator\" title=\"Anchor for Concatenation assignment operator\">#<\/a><\/h2>\n\n\n\n<p>PHP uses the concatenation operator (<code>.<\/code>) to concatenate two strings. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n$greeting = <span class=\"hljs-string\">'Hello '<\/span>;\n$name = <span class=\"hljs-string\">'John'<\/span>;\n\n$greeting = $greeting . $name;\n\n<span class=\"hljs-keyword\">echo<\/span> $greeting;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAgCgokZ3JlZXRpbmcgPSAnSGVsbG8gJzsKJG5hbWUgPSAnSm9obic7CgokZ3JlZXRpbmcgPSAkZ3JlZXRpbmcgLiAkbmFtZTsKCmVjaG8gJGdyZWV0aW5nOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Hello John<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By using the concatenation assignment operator you can concatenate two strings and assigns the result string to a variable. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n$greeting = <span class=\"hljs-string\">'Hello '<\/span>;\n$name = <span class=\"hljs-string\">'John'<\/span>;\n\n$greeting .= $name;\n\n<span class=\"hljs-keyword\">echo<\/span> $greeting;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAgCgokZ3JlZXRpbmcgPSAnSGVsbG8gJzsKJG5hbWUgPSAnSm9obic7CgokZ3JlZXRpbmcgLj0gJG5hbWU7CgplY2hvICRncmVldGluZzs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the assignment operator (<code>=<\/code>) to assign a value to a variable. <\/li>\n\n\n\n<li>The assignment expression returns the value assigned.<\/li>\n\n\n\n<li>Use arithmetic assignment operators to perform arithmetic operations and assign them at the same time.<\/li>\n\n\n\n<li>Use concatenation assignment operator (<code>.=<\/code>) to concatenate strings and assign the result to a variable in a single statement.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"441\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-assignment-operators\/\"\n\t\t\t\tdata-post-title=\"PHP Assignment Operators\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"441\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-assignment-operators\/\"\n\t\t\t\tdata-post-title=\"PHP Assignment Operators\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the PHP assignment operators.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-441","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=441"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/441\/revisions"}],"predecessor-version":[{"id":3348,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/441\/revisions\/3348"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}