The document discusses various operator types in PHP including arithmetic, comparison, logical, and assignment operators. It provides examples of each operator type showing how they are used and the results. Specifically, it defines each operator, shows the syntax using variables, and displays the output of running example code demonstrating the functionality of the different operators in PHP.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
25 views
Lecture 6 - PHP - Operator Types
The document discusses various operator types in PHP including arithmetic, comparison, logical, and assignment operators. It provides examples of each operator type showing how they are used and the results. Specifically, it defines each operator, shows the syntax using variables, and displays the output of running example code demonstrating the functionality of the different operators in PHP.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33
PHP - Operator Types
Dr. Fareed Ahmed Jokhio
Operator Types • What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. PHP language supports following type of operators. – Arithmetic Operators – Comparison Operators – Logical (or Relational) Operators – Assignment Operators – Conditional (or ternary) Operators Arithmetic Operators • There are following arithmetic operators supported by PHP language − • Assume variable A holds 10 and variable B holds 20 then − Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand A - B will give -10 from the first * Multiply both operands A * B will give 200 / Divide numerator by de- B / A will give 2 numerator % Modulus Operator and B % A will give 0 remainder of after an integer division ++ Increment operator, A++ will give 11 increases integer value by one -- Decrement operator, A-- will give 9 decreases integer value by one Arithmetic Operators Example • <html> • • <head> • <title>Arithmetical Operators</title> • </head> • • <body> • • <?php • $a = 42; • $b = 20; • • $c = $a + $b; • echo "Addtion Operation Result: $c <br/>"; • • $c = $a - $b; • echo "Substraction Operation Result: $c <br/>"; • • $c = $a * $b; • echo "Multiplication Operation Result: $c <br/>"; • Arithmetic Operators Example • $c = $a / $b; • echo "Division Operation Result: $c <br/>"; • • $c = $a % $b; • echo "Modulus Operation Result: $c <br/>"; • • $c = $a++; • echo "Increment Operation Result: $c <br/>"; • • $c = $a--; • echo "Decrement Operation Result: $c <br/>"; • ?> • • </body> • </html> Arithmetic Operators Example • This will produce the following result − • Addtion Operation Result: 62 • Substraction Operation Result: 22 • Multiplication Operation Result: 840 • Division Operation Result: 2.1 • Modulus Operation Result: 2 • Increment Operation Result: 42 • Decrement Operation Result: 43 Comparison Operators • There are following comparison operators supported by PHP language • Assume variable A holds 10 and variable B holds 20 then − Comparison Operators Operator Description Example == Checks if the value of two operands are equal or not, if yes (A == B) is not then condition becomes true. true. != Checks if the value of two operands are equal or not, if (A != B) is values are not equal then condition becomes true. true. > Checks if the value of left operand is greater than the value (A > B) is not of right operand, if yes then condition becomes true. true. < Checks if the value of left operand is less than the value of (A < B) is true. right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than or equal (A >= B) is not to the value of right operand, if yes then condition true. becomes true. <= Checks if the value of left operand is less than or equal to (A <= B) is the value of right operand, if yes then condition becomes true. true. Comparison Operators Example • <html> • • <head> • <title>Comparison Operators</title> • </head> • • <body> • • <?php • $a = 42; • $b = 20; • • if( $a == $b ) { • echo "TEST1 : a is equal to b<br/>"; • }else { • echo "TEST1 : a is not equal to b<br/>"; • } • Comparison Operators Example • if( $a > $b ) { • echo "TEST2 : a is greater than b<br/>"; • }else { • echo "TEST2 : a is not greater than b<br/>"; • } • • if( $a < $b ) { • echo "TEST3 : a is less than b<br/>"; • }else { • echo "TEST3 : a is not less than b<br/>"; • } • • if( $a != $b ) { • echo "TEST4 : a is not equal to b<br/>"; • }else { • echo "TEST4 : a is equal to b<br/>"; • } • Comparison Operators Example • if( $a >= $b ) { • echo "TEST5 : a is either greater than or equal to b<br/>"; • }else { • echo "TEST5 : a is neither greater than nor equal to b<br/>"; • } • • if( $a <= $b ) { • echo "TEST6 : a is either less than or equal to b<br/>"; • }else { • echo "TEST6 : a is neither less than nor equal to b<br/>"; • } • ?> • • </body> • </html> Comparison Operators Example • This will produce the following result − • TEST1 : a is not equal to b • TEST2 : a is greater than b • TEST3 : a is not less than b • TEST4 : a is not equal to b • TEST5 : a is either greater than or equal to b • TEST6 : a is neither less than nor equal to b Logical Operators • There are following logical operators supported by PHP language • Assume variable A holds 10 and variable B holds 20 then − Logical Operators Operator Description Example and Called Logical AND operator. If both the operands are (A and B) is true. true then condition becomes true. or Called Logical OR Operator. If any of the two operands (A or B) is true. are non zero then condition becomes true. && Called Logical AND operator. If both the operands are (A && B) is true. non zero then condition becomes true. || Called Logical OR Operator. If any of the two operands (A || B) is true. are non zero then condition becomes true. ! Called Logical NOT Operator. Use to reverses the !(A && B) is false. logical state of its operand. If a condition is true then Logical NOT operator will make false. Logical Operators Example • <html> • • <head> • <title>Logical Operators</title> • </head> • • <body> • • <?php • $a = 42; • $b = 0; • • if( $a && $b ) { • echo "TEST1 : Both a and b are true<br/>"; • }else{ • echo "TEST1 : Either a or b is false<br/>"; • } Logical Operators Example • • if( $a and $b ) { • echo "TEST2 : Both a and b are true<br/>"; • }else{ • echo "TEST2 : Either a or b is false<br/>"; • } • • if( $a || $b ) { • echo "TEST3 : Either a or b is true<br/>"; • }else{ • echo "TEST3 : Both a and b are false<br/>"; • } • • if( $a or $b ) { • echo "TEST4 : Either a or b is true<br/>"; • }else { • echo "TEST4 : Both a and b are false<br/>"; • } • Logical Operators Example • $a = 10; • $b = 20; • • if( $a ) { • echo "TEST5 : a is true <br/>"; • }else { • echo "TEST5 : a is false<br/>"; • } • • if( $b ) { • echo "TEST6 : b is true <br/>"; • }else { • echo "TEST6 : b is false<br/>"; • } • Logical Operators Example • if( !$a ) { • echo "TEST7 : a is true <br/>"; • }else { • echo "TEST7 : a is false<br/>"; • } • • if( !$b ) { • echo "TEST8 : b is true <br/>"; • }else { • echo "TEST8 : b is false<br/>"; • } • ?> • • </body> • </html> Logical Operators Example • This will produce the following result − • TEST1 : Either a or b is false • TEST2 : Either a or b is false • TEST3 : Either a or b is true • TEST4 : Either a or b is true • TEST5 : a is true • TEST6 : b is true • TEST7 : a is false • TEST8 : b is false Assignment Operators • There are following assignment operators supported by PHP language − Operator Description Example = Simple assignment operator, Assigns values from C = A + B will assign right side operands to left side operand value of A + B into C += Add AND assignment operator, It adds right operand C += A is equivalent to the left operand and assign the result to left to C = C + A operand -= Subtract AND assignment operator, It subtracts right C -= A is equivalent operand from the left operand and assign the result to C = C - A to left operand *= Multiply AND assignment operator, It multiplies C *= A is equivalent right operand with the left operand and assign the to C = C * A result to left operand /= Divide AND assignment operator, It divides left C /= A is equivalent operand with the right operand and assign the to C = C / A result to left operand %= Modulus AND assignment operator, It takes C %= A is equivalent modulus using two operands and assign the result to C = C % A to left operand Assignment Operators Example • <html> • • <head> • <title>Assignment Operators</title> • </head> • • <body> • • <?php • $a = 42; • $b = 20; • • $c = $a + $b; • echo "Addtion Operation Result: $c <br/>"; • • $c += $a; • echo "Add AND Assigment Operation Result: $c <br/>"; • Assignment Operators Example • $c -= $a; • echo "Subtract AND Assignment Operation Result: $c <br/>"; • • $c *= $a; • echo "Multiply AND Assignment Operation Result: $c <br/>"; • • $c /= $a; • echo "Division AND Assignment Operation Result: $c <br/>"; • • $c %= $a; • echo "Modulus AND Assignment Operation Result: $c <br/>"; • ?> • • </body> • </html> Assignment Operators Example • This will produce the following result − • Addtion Operation Result: 62 • Add AND Assigment Operation Result: 104 • Subtract AND Assignment Operation Result: 62 • Multiply AND Assignment Operation Result: 2604 • Division AND Assignment Operation Result: 62 • Modulus AND Assignment Operation Result: 20 Conditional Operator • There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax − Conditional Operator Operator Description Example ?: Conditional Expression If Condition is true ? Then value X : Otherwise value Y Conditional Operator Example • <html> • • <head> • <title>Arithmetical Operators</title> • </head> • • <body> • • <?php • $a = 10; • $b = 20; • • /* If condition is true then assign a to result otheriwse b */ • $result = ($a > $b ) ? $a :$b; • • echo "TEST1 : Value of result is $result<br/>"; Conditional Operator Example • • /* If condition is true then assign a to result otheriwse b */ • $result = ($a < $b ) ? $a :$b; • • echo "TEST2 : Value of result is $result<br/>"; • ?> • • </body> • </html> Conditional Operator Example • This will produce the following result − • TEST1 : Value of result is 20 • TEST2 : Value of result is 10 Operators Categories • All the operators we have discussed above can be categorised into following categories − • Unary prefix operators, which precede a single operand. • Binary operators, which take two operands and perform a variety of arithmetic and logical operations. • The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression. • Assignment operators, which assign a value to a variable. Precedence of PHP Operators • Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator − • For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7. • Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. Precedence of PHP Operators Category Operator Associativity Unary ! ++ -- Right to left Multiplicative */% Left to right Additive +- Left to right Relational < <= > >= Left to right Equality == != Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= Right to left