{"id":15438,"date":"2020-06-26T17:20:53","date_gmt":"2020-06-26T17:20:53","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15438"},"modified":"2022-04-28T09:15:24","modified_gmt":"2022-04-28T09:15:24","slug":"what-are-the-operators-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-are-the-operators-in-php\/","title":{"rendered":"What are the Operators in PHP?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>What is Operators in PHP ?<\/strong><\/h3>\n\n\n\n<p>Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an operator that tells PHP to add two variables or values, while the greater-than (&gt;) symbol is an operator that tells PHP to compare two values.<\/p>\n\n\n\n<p>The following lists describe the different operators used in PHP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Arithmetic Operators<\/h3>\n\n\n\n<p>Arithmetic operators are used to performing common arithmetical operations, such as addition, subtraction, multiplication, etc. Here&#8217;s a complete list of PHP&#8217;s arithmetic operators:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Description<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Result<\/span><\/strong><\/td><\/tr><tr><td>+<\/td><td>Addition<\/td><td>$x + $y<\/td><td>Sum of $x and $y<\/td><\/tr><tr><td>&#8211;<\/td><td>Subtraction<\/td><td>$x &#8211; $y<\/td><td>Difference of $x and $y.<\/td><\/tr><tr><td>*<\/td><td>Multiplication<\/td><td>$x * $y<\/td><td>Product of $x and $y.<\/td><\/tr><tr><td>\/<\/td><td>Division<\/td><td>$x \/ $y<\/td><td>Quotient of $x and $y<\/td><\/tr><tr><td>%<\/td><td>Modulus<\/td><td>$x % $y<\/td><td>Remainder of $x divided by $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these arithmetic operators in action:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/03cd59a8f7992ff159d2f74fddc20fb2.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Assignment Operators<\/h3>\n\n\n\n<p>The assignment operators are used to assign values to variables.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Description<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Is The Same As<\/span><\/strong><\/td><\/tr><tr><td>=<\/td><td>Assign<\/td><td>$x = $y<\/td><td>$x = $y<\/td><\/tr><tr><td>+=<\/td><td>Add and assign<\/td><td>$x += $y<\/td><td>$x = $x + $y<\/td><\/tr><tr><td>-=<\/td><td>Subtract and assign<\/td><td>$x -= $y<\/td><td>$x = $x &#8211; $y.<\/td><\/tr><tr><td>*=<\/td><td>Multiply and assign<\/td><td>$x *= $y<\/td><td>$x = $x * $y<\/td><\/tr><tr><td>\/=<\/td><td>Divide and assign quotient<\/td><td>$x \/= $y<\/td><td>$x = $x \/ $y<\/td><\/tr><tr><td>%=<\/td><td>Divide and assign modulus<\/td><td>$x %= $y<\/td><td>$x = $x % $y<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div><\/div>\n\n\n\n<p>The following example will show you these assignment operators in action:<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/aaf17b48b37001327562c06614b78fcc.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Comparison Operators<\/h3>\n\n\n\n<p>The comparison operators are used to compare two values in a Boolean fashion.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/strong><\/td><td><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Description<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/strong><\/td><td><strong><span class=\"has-inline-color has-vivid-red-color\">Result<\/span><\/strong><\/td><\/tr><tr><td>==<\/td><td><\/td><td>Equal<\/td><td>$x == $y<\/td><td>True if $x is equal to $y<\/td><\/tr><tr><td>===<\/td><td><\/td><td>Identical<\/td><td>$x === $y<\/td><td>True if $x is equal to $y, and they are of the same type<\/td><\/tr><tr><td>!=<\/td><td><\/td><td>Not equal<\/td><td>$x != $y<\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td>&lt;&gt;<\/td><td><\/td><td>Not equal<\/td><td>$x &lt;&gt; $y<\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td>!==<\/td><td><\/td><td>Not identical<\/td><td>$x !== $y<\/td><td>True if $x is not equal to $y, or they are not of the same type<\/td><\/tr><tr><td>&lt;<\/td><td><\/td><td>Less than<\/td><td>$x &lt; $y<\/td><td>True if $x is less than $y<\/td><\/tr><tr><td>&gt;<\/td><td><\/td><td>Greater than<\/td><td>$x &gt; $y<\/td><td>True if $x is greater than $y<\/td><\/tr><tr><td>&gt;=<\/td><td><\/td><td>Greater than or equal to<\/td><td>$x &gt;= $y<\/td><td>True if $x is greater than or equal to $y<\/td><\/tr><tr><td>&lt;=<\/td><td><\/td><td>Less than or equal to<\/td><td>$x &lt;= $y<\/td><td>True if $x is less than or equal to $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these comparison operators in action:<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/64d118da187dfd5d63320ace92742ccf.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Incrementing and Decrementing Operators<\/h3>\n\n\n\n<p>The increment\/decrement operators are used to increment\/decrement a variable&#8217;s value.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/td><td class=\"has-text-align-center\" data-align=\"center\"><span class=\"has-inline-color has-vivid-red-color\">Name<\/span><\/td><td class=\"has-text-align-center\" data-align=\"center\"><span class=\"has-inline-color has-vivid-red-color\">Effect<\/span><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">++$x<\/td><td class=\"has-text-align-center\" data-align=\"center\">Pre-increment<\/td><td class=\"has-text-align-center\" data-align=\"center\">Increments $x by one, then returns $x<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">$x++<\/td><td class=\"has-text-align-center\" data-align=\"center\">Post-increment<\/td><td class=\"has-text-align-center\" data-align=\"center\">Returns $x, then increments $x by one<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">&#8211;$x<\/td><td class=\"has-text-align-center\" data-align=\"center\">Pre-decrement<\/td><td class=\"has-text-align-center\" data-align=\"center\">Decrements $x by one then returns $x<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">$x&#8211;<\/td><td class=\"has-text-align-center\" data-align=\"center\">Post-decrement<\/td><td class=\"has-text-align-center\" data-align=\"center\">Returns $x, then decrements $x by one<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these increment and decrement operators in action:<\/p>\n\n\n\n<p><strong>Example:-<\/strong> <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/36d8b775ca4a1ba268b999a24ea52002.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Logical Operators<\/h3>\n\n\n\n<p>The logical operators are typically used to combine conditional statements.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Name<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/td><td class=\"has-text-align-center\" data-align=\"center\"><span class=\"has-inline-color has-vivid-red-color\">Result<\/span><\/td><\/tr><tr><td>and<\/td><td>And<\/td><td>$x and $y<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if both $x and $y are true<\/td><\/tr><tr><td>or<\/td><td>Or<\/td><td>$x or $y<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if either $x or $y is true<\/td><\/tr><tr><td>xor<\/td><td>Xor<\/td><td>$x xor $y<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if either $x or $y is true, but not both<\/td><\/tr><tr><td>&amp;&amp;<\/td><td>And<\/td><td>$x &amp;&amp; $y<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if both $x and $y are true<\/td><\/tr><tr><td>||<\/td><td>Or<\/td><td>$x || $y<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if either $$x or $y is true<\/td><\/tr><tr><td>!<\/td><td>Not<\/td><td>!$x<\/td><td class=\"has-text-align-center\" data-align=\"center\">True if $x is not true<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these logical operators in action:<\/p>\n\n\n\n<p><strong>Example=&gt;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/2de6016fd943634df86c1f61a514120f.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP String Operators<\/h3>\n\n\n\n<p>There are two operators which are specifically designed for strings.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Description<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Result<\/span><\/td><\/tr><tr><td>.<\/td><td>Concatenation<\/td><td>$str1 . $str2 <\/td><td>Concatenation of $str1 and $str2<\/td><\/tr><tr><td>.=<\/td><td>Concatenation assignment<\/td><td>$str1 .= $str2<\/td><td>Appends the $str2 to the $str1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these string operators in action:<\/p>\n\n\n\n<p><strong>Example=&gt;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/7be2f3e8734c6c0af188b050ef140414.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Array Operators<\/h3>\n\n\n\n<p>The array operators are used to compare arrays:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>True if $x is not equal to $y<span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Name<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Example<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Result<\/span><\/td><\/tr><tr><td>+<\/td><td>Union<\/td><td>$x + $y<\/td><td>Union of $x and $y<\/td><\/tr><tr><td>==<\/td><td>Equality<\/td><td>$x == $y<\/td><td>True if $x and $y have the same key\/value pairs<\/td><\/tr><tr><td>===<\/td><td>Identity<\/td><td>$x === $y<\/td><td>True if $x and $y have the same key\/value pairs in the same order and of the same types<\/td><\/tr><tr><td>!=<\/td><td>Inequality<\/td><td>$x != $y <\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td>&lt;&gt;<\/td><td>Inequality<\/td><td>$x &lt;&gt; $y<\/td><td>True if $x is not equal to $y<\/td><\/tr><tr><td>!==<\/td><td>Non-identity<\/td><td>$x !== $y<\/td><td>True if $x is not identical to $y<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you these array operators in action:<\/p>\n\n\n\n<p><strong>Example=&gt;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/f3781048782a8acc83bc4ff2c0918b7b.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">PHP Spaceship Operator<\/h3>\n\n\n\n<p>The spaceship operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater. It basically provides three-way comparison as shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><span class=\"has-inline-color has-vivid-red-color\">Operator<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">&lt;=&gt; Equivalent<\/span><\/td><\/tr><tr><td>$x &lt; $y<\/td><td>($x &lt;=&gt; $y) === -1<\/td><\/tr><tr><td>$x &lt;= $y<\/td><td>($x &lt;=&gt; $y) === -1 || ($x &lt;=&gt; $y) === 0<\/td><\/tr><tr><td>$x == $y<\/td><td>($x &lt;=&gt; $y) === 0<\/td><\/tr><tr><td>$x != $y<\/td><td>($x &lt;=&gt; $y) !== 0<\/td><\/tr><tr><td>$x &gt;= $y<\/td><td>($x &lt;=&gt; $y) === 1 || ($x &lt;=&gt; $y) === 0<\/td><\/tr><tr><td>$x &gt; $y<\/td><td>$x &lt;=&gt; $y) === 1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following example will show you how spaceship operator actually works:<\/p>\n\n\n\n<p><strong>Example=&gt;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/22ea1f501c64e3e79715f04ccd36960e.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_49950\"><iframe loading=\"lazy\"  id=\"_ytid_37968\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_49950\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>What is Operators in PHP ? Operators are symbols that tell the PHP processor to perform certain actions. For example, the addition (+) symbol is an operator that tells PHP&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[1],"tags":[5921,6189,6184,6185,6186,6187,5849,6190,6188],"class_list":["post-15438","post","type-post","status-publish","format-standard","hentry","category-sql","tag-arithmetic-operators","tag-array-operators","tag-assignment-operators","tag-comparison-operators","tag-incrementing-and-decrementing-operators","tag-logical-operators","tag-operators","tag-spaceship-operator","tag-string-operators"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15438"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15438\/revisions"}],"predecessor-version":[{"id":24711,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15438\/revisions\/24711"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}