Expressions in PHP

 Expressions in PHP

·         An expression is a combination series of variables, operands, operators, and method calls (according to the language syntax) that calculates to a single value.

·         Compound expressions are combining expressions according to their types use by operators.

·         Compound expressions should be explicit and indicate with parentheses which operators should be evaluated first.



There are four types of expressions exist in PHP:

  • Arithmetic expressions
  • Relational expressions
  • Logical expressions
  • Conditional expressions

 

Arithmetic Expressions

An arithmetic expression consists of operands and arithmetic operators.

Computes a value of type int, float or double.

an expression contains only integral operands, then it is known as pure integer expression when it contains only real operands, it is known as pure real expression, and when it contains both integral and real operands, it is known as mixed-mode expression.

Evaluation of Arithmetic Expressions

The expressions are evaluated by performing one operation at a time.

The precedence and associativity of operators decide the order of the evaluation of individual operations.

example: -     6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)

 

Evaluation of expression

Description of each operation

6*2/( 2+1 * 2/3 +6) +8 * (8/4)

An expression is given.

6*2/(2+2/3 + 6) + 8 * (8/4)

2 is multiplied by 1, giving the value 2.

6*2/(2+0+6) + 8 * (8/4)

2 is divided by 3, giving the value 0.

6*2/ 8+ 8 * (8/4)

2 is added to 6, giving a value of 8.

6*2/8 + 8 * 2

8 is divided by 4, giving value 2.

12/8 +8 * 2

6 is multiplied by 2, giving the value 12.

1 + 8 * 2

12 is divided by 8, giving value 1.

1 + 16

8 is multiplied by 2, giving the value 16.

17

1 is added to 16, giving a value of 17.

 

 









Relational Expressions

  • A relational expression is an expression used to compare two operands.
  • It is a condition that is used to decide whether the action should be taken or not.
  • In relational expressions, a numeric value cannot be compared with the string value.
  • The result of the relational expression can be either zero or non-zero value. Here, the zero value is equivalent to a false, and the non-zero value is equivalent to a true.

 

 

Relational Expression

Description

x%2 = = 0

This condition is used to check whether the x is an even number or not. The relational expression results in value 1 if x is an even number otherwise results in value 0.

a!=b

It is used to check whether a is not equal to b. This relational expression results in 1 if a is not equal to b otherwise 0.

a+b = = x+y

the expression "a+b" is equal to the expression "x+y".

a>=9

the value of a is greater than or equal to 9.








Logical Expressions

  • A logical expression is an expression that computes either a zero or non-zero value.
  • It is a complex test condition to take a decision.

example of the logical expressions.

 

 

Logical Expressions

Description

( x > 4 ) && ( x < 6 )

the x is greater than 4 and x is less than 6. The result of the condition is true only when both conditions are true.

x > 10 || y <11

x is greater than 10 or y is less than 11.

The result of the test condition is true if either of the conditions holds a true value.

! ( x > 10 ) && ( y = = 2 )

x is not greater than 10 and y is equal to 2. The result of the condition is true if both conditions are true.

 







Conditional Expressions

  • A conditional expression is an expression that returns 1 if the condition is true otherwise 0.
  • A conditional operator is also known as a ternary operator.

 

Syntax :-

exp1 ? exp2 : exp3

 

A conditional expression is evaluated on the basis of the value of the exp1 expression. If the condition of the expression exp1 is true, then the final conditional expression is represented by exp2 otherwise represented by exp3.

 ===================================================================


Post a Comment

0 Comments