
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Expressions
Introduction
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 (=)
Syntax
$x=100; //100 is an expression $a=$b+$c; //b+$c is an expression $c=add($a,$b); //add($a,$b) is an expresson $val=sqrt(100); //sqrt(100) is an expression $var=$x!=$y; //$x!=$y is an expression
expression with ++ and -- operators
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
Both prefix and postfix ++ operators increment value of operand by 1 (whereas -- 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
Uses postfix ++ operator
Example
<?php $x=10; $y=$x++; //equivalent to $y=$x followed by $x=$x+1 echo "x = $x y = $y"; ?>
Output
This produces following result
x = 11 y = 10
Whereas following example uses prefix increment operator in assignment
Example
<?php $x=10; $y=++$x;; //equivalent to $x=$x+1 followed by $y=$x echo "x = $x y = $y"; ?>
Output
This produces following result
x = 11 y = 11
Expression with Ternary conditional operator
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
Example
<?php $marks=60; $result= $marks<50 ? "fail" : "pass"; echo $result; ?>
Output
Following result will be displayed
pass
- Related Articles
- Generator Return Expressions in PHP 7
- JavaScript Regular Expressions
- Java Regular Expressions Tutorial
- Lambda Expressions in C#
- Trigonometric expressions in Arduino
- Exponential expressions in Arduino
- What is Regular Expressions?
- What are Boolean Expressions?
- What are Algebra Expressions?
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions
- Lookbehind Assertions JavaScript Regular Expressions
- How to factorize algebraic expressions?
