{"id":10880,"date":"2020-02-21T05:37:35","date_gmt":"2020-02-21T05:37:35","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=10880"},"modified":"2021-11-12T10:25:33","modified_gmt":"2021-11-12T10:25:33","slug":"logical-increment-decrement-and-string-operators-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/logical-increment-decrement-and-string-operators-in-php\/","title":{"rendered":"Logical, Increment, decrement, and String Operators in PHP."},"content":{"rendered":"\n<h1 class=\"has-text-align-center wp-block-heading\">Logical Operators<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">1.  Logical AND(&amp;&amp;\/and).<\/h3>\n\n\n\n<p>There are two Logical AND Operators, First <em><strong>&amp;&amp;<\/strong><\/em> and 2nd <strong><em>and<\/em><\/strong> where First Operator has higher precedence(means higher priority) than 2nd. It Compares two expressions or operators and gives output in 4 cases. <em>See the Below image.<\/em><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"434\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-AND-1024x434.png\" alt=\"\" class=\"wp-image-10887\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-AND-1024x434.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-AND-300x127.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-AND-768x325.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-AND.png 1067w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><strong>Case 1.<\/strong> When Both the expressions are <em>true<\/em> then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :- <\/strong>  <em> (5&gt;2)&amp;&amp;(5&lt;8)  <\/em><\/p>\n\n\n\n<p>In the above Example, It returns <strong><em>True<\/em><\/strong> because both the expressions are <em>true<\/em>.<\/p>\n\n\n\n<p><strong>Case 2.<\/strong> When 1st expression is <em>true <\/em>and 2nd is <em>false <\/em>then it returns <strong><em>False<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(3&lt;5)&amp;&amp;(5&lt;2)<\/em><\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>true<\/em> but 2nd expression is <em>false<\/em> then it returns <strong><em>False<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Case 3.<\/strong> When 1st expression is <em>false<\/em> and 2nd is <em>true <\/em>then it returns <strong><em>False<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(5&lt;2)&amp;&amp;(3&lt;5)<\/em> <\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>false<\/em> but 2nd expression is <em>true<\/em> then it returns <strong><em>False<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Case 4.<\/strong> When Both the Expression is <em>false<\/em> then it also returns <em><strong>False<\/strong><\/em>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(6&lt;2)&amp;&amp;(3&gt;5)<\/em><\/p>\n\n\n\n<p>In the above Example, Both expressions are <em>false<\/em> then it returns <strong><em>False<\/em><\/strong>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Logical OR(<em><strong>||<\/strong><\/em>\/or).<\/h3>\n\n\n\n<p>There are Two Logical OR, First <strong>||<\/strong> and 2nd <strong><em>or<\/em><\/strong> where First Operator has higher precedence(means higher priority)  than 2nd. It Compares two expressions or operators and gives output in 4 cases. <em>See the Below image.<\/em> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-OR-1024x439.png\" alt=\"\" class=\"wp-image-10891\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-OR-1024x439.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-OR-300x129.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-OR-768x329.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Logical-OR.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Case 1.<\/strong> When Both the expressions are <em>true<\/em> then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>    <em>(5&gt;2)<strong>||<\/strong>(5&lt;8)<\/em><\/p>\n\n\n\n<p>In the above Example, It returns <strong><em>True<\/em><\/strong> because both the expressions are <em>true<\/em>.<\/p>\n\n\n\n<p><strong>Case 2.<\/strong> When 1st expression is <em>true <\/em>and 2nd is <em>false <\/em>then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(3&lt;5)<strong>||<\/strong>(5&lt;2)<\/em><\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>true<\/em> but 2nd expression is <em>false<\/em> then it returns <strong><em>True<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Case 3.<\/strong> When 1st expression is <em>false<\/em> and 2nd is <em>true <\/em>then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(5&lt;2)<\/em><strong>||<\/strong><em>(3&lt;5)<\/em> <\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>false<\/em> but 2nd expression is <em>true<\/em> then it returns <strong><em>True<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Case 4.<\/strong> When Both the Expression is <em>false<\/em> then it also returns <em><strong>False<\/strong><\/em>. For <\/p>\n\n\n\n<p><strong>Example :-<\/strong>  <em>(6&lt;2)<\/em><strong>||<\/strong><em>(3&gt;5)<\/em><\/p>\n\n\n\n<p>In the above Example, Both expressions are <em>false<\/em> then it returns <strong><em>False<\/em><\/strong>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Logical NOT(!).<\/h3>\n\n\n\n<p>It returns the reverse value of expression means when the expression is <em>true<\/em> then it returns <strong><em>False<\/em><\/strong>, and when the expression is <em>false<\/em> then it returns <strong><em>True.<\/em><\/strong> <\/p>\n\n\n\n<p><strong>For Example :-<\/strong> <em> !(3&lt;2)<\/em><\/p>\n\n\n\n<p>In the above example, the expression is <em>false<\/em> but it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Exclusive OR(xor).<\/h3>\n\n\n\n<p>When both the expression are Identical (means both are <em>true<\/em> or <em>false<\/em>) then it returns <strong><em>False<\/em><\/strong> and when any one of them is <em>true<\/em> then it returns <strong><em>True<\/em><\/strong>. <em>See the below image.<\/em><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"399\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Exclusive-OR.png\" alt=\"\" class=\"wp-image-10900\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Exclusive-OR.png 975w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Exclusive-OR-300x123.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/02\/Exclusive-OR-768x314.png 768w\" sizes=\"auto, (max-width: 975px) 100vw, 975px\" \/><\/figure><\/div>\n\n\n\n<p><strong>Case 1.<\/strong> When Both the expressions are <em>true<\/em> or <em>false<\/em> then it returns <strong><em>False<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>    <em>(5&gt;2)xor(5&lt;8)<\/em>  or  <em>(6&lt;2)<\/em><strong>||<\/strong><em>(3&gt;5)<\/em> <\/p>\n\n\n\n<p>In the above example, both the expressions in the first example is <em>true<\/em> and both the expressions in the second example is <em>false<\/em> so both the examples return <em><strong>False<\/strong><\/em>. <\/p>\n\n\n\n<p><strong>Case 2.<\/strong> When 1st expression is <em>true <\/em>and 2nd is <em>false <\/em>then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :- <\/strong> <em>(3&lt;5)xor(5&lt;2)<\/em><\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>true<\/em> but 2nd expression is <em>false<\/em> then it returns <strong><em>True<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Case 3.<\/strong> When 1st expression is <em>false<\/em> and 2nd is <em>true <\/em>then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<p><strong>For Example :-<\/strong>  <em>(5&lt;2)xor(3&lt;5)<\/em> <\/p>\n\n\n\n<p>In the above Example, 1st expression is <em>false<\/em> but 2nd expression is <em>true<\/em> then it returns <strong><em>True<\/em><\/strong>. <\/p>\n\n\n\n<h1 class=\"has-text-align-center wp-block-heading\">Increment and Decrement Operators.<\/h1>\n\n\n\n<p><strong><em>Increment Operator:- <\/em><\/strong> It increases the value of the variable by 1. It is of two types:<\/p>\n\n\n\n<p><strong><em>1. Pre-increment:- <\/em><\/strong> It first increases the value of variable, then uses the new variable value. <\/p>\n\n\n\n<p><strong>For Example:-<\/strong>  <strong>++$a<\/strong>  <\/p>\n\n\n\n<p><em>&lt;?php<br>     $a=40;<br>     echo  ++$a;<br>         ?&gt;<\/em><\/p>\n\n\n\n<p><strong>Output :-<\/strong>  41<\/p>\n\n\n\n<p>2. <strong><em>Post-increment:-<\/em><\/strong> First uses the current value of the variable, then increases the value of variable by 1.<\/p>\n\n\n\n<p><strong>For Example:-<\/strong>  <strong>$a++<\/strong><\/p>\n\n\n\n<p><em>&lt;?php<br>     $a=25;<br>     echo $a++.&#8221;&lt;br&gt;&#8221;;<br>     echo $a;<br>         ?&gt;<\/em><\/p>\n\n\n\n<p><strong>Output:-<\/strong>   25<br>                   26 <\/p>\n\n\n\n<p><strong><em>Decrement Operator:-<\/em><\/strong>   It decreases the value of the variable by 1. It is of two types: <\/p>\n\n\n\n<p> <strong><em>1. Pre-decrement:- <\/em><\/strong> It first decreases the value of the variable, then uses the new variable value.  <\/p>\n\n\n\n<p> <strong>For Example:-<\/strong>  <strong> &#8211; &#8211; $a<\/strong>   <\/p>\n\n\n\n<p><em>&lt;?php<br>     $a=40;<br>     echo  &#8211; &#8211; $a;<br>         ?&gt;<\/em><\/p>\n\n\n\n<p><strong>Output :-<\/strong>  39<\/p>\n\n\n\n<p>2. <strong><em>Post-decrement:-<\/em><\/strong> First uses the current value of the variable, then decreases the value of the variable by 1.<\/p>\n\n\n\n<p><strong>For Example:-<\/strong>  <strong>$a- &#8211;<\/strong><\/p>\n\n\n\n<p><em>&lt;?php<br>     $a=25;<br>     echo $a &#8211; -.&#8221;&lt;br&gt;&#8221;;<br>     echo $a;<br>         ?&gt;<\/em><\/p>\n\n\n\n<p><strong>Output:-<\/strong>   25<br>                   24 <\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_74458\"><iframe loading=\"lazy\"  id=\"_ytid_51551\"  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_74458\"  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>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logical Operators 1. Logical AND(&amp;&amp;\/and). There are two Logical AND Operators, First &amp;&amp; and 2nd and where First Operator has higher precedence(means higher priority) than 2nd. It Compares two expressions&#8230; <\/p>\n","protected":false},"author":19,"featured_media":11038,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[],"class_list":["post-10880","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/10880","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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=10880"}],"version-history":[{"count":30,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/10880\/revisions"}],"predecessor-version":[{"id":24936,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/10880\/revisions\/24936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/11038"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=10880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=10880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=10880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}