{"id":58,"date":"2023-05-06T02:55:48","date_gmt":"2023-05-06T02:55:48","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.com\/?p=58"},"modified":"2023-05-06T02:55:48","modified_gmt":"2023-05-06T02:55:48","slug":"what-are-expressions-in-php","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2023\/05\/06\/what-are-expressions-in-php\/","title":{"rendered":"What are expressions in PHP?"},"content":{"rendered":"\n<p>Almost everything in a PHP script is an expression. Anything that has a value is an expression. In a typical assignment statement ($x=100), a literal value, a function or operands processed by operators is an expression, anything that appears to the right of assignment operator (=)<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p>$x=100; \/\/100 is an expression<\/p>\n\n\n\n<p>$a=$b+$c; \/\/b+$c is an expression<\/p>\n\n\n\n<p>$c=add($a,$b); \/\/add($a,$b) is an expression<\/p>\n\n\n\n<p>$val=sqrt(100); \/\/sqrt(100) is an expression<\/p>\n\n\n\n<p>$var=$x!=$y; \/\/$x!=$y is an expression<\/p>\n\n\n\n<p><strong>Expression with ++ and &#8212; operators<\/strong><\/p>\n\n\n\n<p>These operators are called increment and decrement operators respectively. They are unary operators, needing just one operand and can be used in prefix or postfix manner, although with different effect on value of expression<\/p>\n\n\n\n<p>Both prefix and postfix ++ operators increment the value of operand by 1 (whereas &#8212; operator decrements by 1). However, when used in assignment expression, prefix makesincremnt\/decrement first and then followed by assignment. In case of postfix, assignment is done before increment\/decrement<\/p>\n\n\n\n<p>Uses postfix ++ operator<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$x=10;<\/p>\n\n\n\n<p>$y=$x++; \/\/equivalent to $y=$x followed by $x=$x+1<\/p>\n\n\n\n<p>echo &#8220;x = $x y = $y&#8221;;<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>This produces following result<\/p>\n\n\n\n<p>x = 11 y = 10<\/p>\n\n\n\n<p>Whereas following example uses prefix increment operator in assignment<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$x=10;<\/p>\n\n\n\n<p>$y=++$x;; \/\/equivalent to $x=$x+1 followed by $y=$x<\/p>\n\n\n\n<p>echo &#8220;x = $x y = $y&#8221;;<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>This produces following result<\/p>\n\n\n\n<p>x = 11 y = 11<\/p>\n\n\n\n<p><strong>Expression with Ternary conditional operator<\/strong><\/p>\n\n\n\n<p>Ternary operator has three operands. First one is a logical expression. If it is TRU, second operand expression is evaluated otherwise third one is evaluated<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>$marks=60;<\/p>\n\n\n\n<p>$result= $marks&lt;50 ? &#8220;fail&#8221; : &#8220;pass&#8221;;<\/p>\n\n\n\n<p>echo $result;<\/p>\n\n\n\n<p>?&gt;<\/p>\n\n\n\n<p><strong>Output<\/strong> : pass<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Almost everything in a PHP script is an expression. Anything that has a value is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}