Hi Everybody, today we are going to learn, how operators can help us to make things helpful for us 🙂 .
There are various kind of operators, we are going to study few of most important operators;
-
Assignment Operators
Assignment operators are used with numeric values to write a value to a variable. The basic assignment operator is “=”. Left operand gets set to the value of the assignment expression on the right.
Assignment Similar x = y x = y x += y x = x + y x -= y x = x – y x *= y x = x * y x /= y x = x / y x %= y x = x % y As given in the table you can see that left section is similar to the right section. it means we can replace x=x+y with x+=y. so we can use the assignment operator with arithmetic operators in this way.
-
String Operators
a.) Concatenation(.) :
It is used for concatenating two strings. suppose we want to concatenate two strings str1 and str2 and store it in str3.
Example:$str3 = $str1 . $str2;
b.) Concatenation assignment (.=) :
It is used for appending a string into the other string. suppose there is a str1, want to append str2 into it, will write it like as;Example: $str1 .= $str2;
-
Conditional Assignment Operators
Conditional assignment operators are used to set a value depending on conditions:
a.) Ternary (?:) : It is used as if-else condition.
Example: $x = expr1 ? expr2 : expr3
Explanation: if expr1 is true then the value of x will be expr2 otherwise expr3.
b.) Null coalescing(??) :
Example: $x = expr1 ?? expr2
Explanation: if the expr1 exists and does not equal to NULL then the value of x will be expr1 otherwise expr2.
Hope it helps you to learn something new 🙂 Thanks!