Operator and Expression in PHP (Part 2)

Operator and Expression play an important role in PHP programming. So, to be an expert PHP programmer you have to have clear concept about PHP operator and expression. The basic concept of PHP operator and expression was explained in the previous article. So, this article will explain only the remaining part of PHP operator and expression.

Operators and Expressions in PHP

Operators and Expressions in PHP

Assignment Operator

Assignment operators are used to assign any value into a variable. These operators can be used in following seven ways.

  • Equal to (=): Simply it is used to put any value to any variable. Example: $a=5;
  • Plus then equal to (+=): This operator adds a numeric value with the previous value of a variable and then keeps the result into that variable. Such as…
    $a=5;
    $a+=10;
    Now $a contains 15. The expression can also be expressed as…
    $a=$a+5;
    This expression expresses that the numeric value 5 will be added with the previous value of $a and then the result will be assign into $a.
  • Minus then equal to (-=): This operator subtracts a numeric value from the previous value of a variable and then keeps the result into that variable. Example:
    $a=5;
    $a-=3
    Now $a contains 2.
  • Divide then equal to: This operator divides a variable value by a numeric value and then put the result into that variable. Example:
    $a=10;
    $a/=2;
    Now $a contains 5.
  • Multiply then equal to: This operator multiplies a numeric variable value with the previous value of a variable and then keeps the result into that variable. Example:
    $a=5;
    $a*=5;
    So, $a is now 25.
  • Modulus then equal to: This operator divides a variable value by a numeric value and then keeps the remainder into that variable. Example:
    $a=22;
    $a%=7;
    Now $a contains 1.
  • Concatenate then equal to(. =): This operator concatenates a new string value with the previous string value of variable and then keeps the resultant string value into that variable. Example:
    $a=”My name is”;
    $a. =”Tom”;
    Now $a contains “My name is Tom” string value.

Comparison or Relational Operator

These types of operator return either true or false value. Comparison or Relational Operator can be divided into seven categories.

  • Double equal (==): This operator is used to compare any two values without case sensitivity or data type. If two values are equal, it returns true otherwise it returns false. Example:
    $a=’A’;
    $b=’a’;
    Now if $a is compared with $b with double equal operator, it will return true because double equal compares without case sensitivity and data type.
  • Triple equal or Identical (===): This operator is used to compare any two values with case sensitivity and data types. Identical operator compares two values so hardly. Comparing values must be equal with case and data type otherwise it will return false. Example:
    $a=’A’;
    $b=’a’;
    Now if $a is compared with $b with identical operator, it will return false value because variable $a and variable $b are not exactly same. Now see below example:
    $a=’A’;
    $b=’A’;
    Now comparison will return true value because two variables contain exactly same value. Identical operator is mostly used to compare password value in PHP developing environment. So, when you will develop any site or program where password need to be compared, you must use identical operator otherwise your development will be very poor.
  • Not equal to(!= or <>): This operator returns a deep logical result. See below example…
    $a = 5;
    Now if you express $a != 5, the result will be false because you have assigned variable $a=5 but expression says variable $a not equal 5. So, program will return a false value because your expression is going against with your declaring variable value. Again, if you assign $a = 4 and the expression is $a != 5, the result will be true because $a = 4 and expression is saying variable $a is not equal 5. Now, your expression is true with declaring value. So, The operator will return a true value.
  • Less than (<): This operator is used to compare any variable value with a given numeric value. If the value of the variable is smaller than the given value, it will return true otherwise it will return false. Say, you have assigned $a =5 and your expression is $a < 10, then your program will return true because here $a is obviously smaller than 10. But if you express $a < 4, you will find a false value because 4 is not smaller than 5.
  • Greater than (>): This operator is used to compare any variable value with a given numeric value. If left side value is greater than right side value, a Boolean true is returned otherwise Boolean false is returned. Say, you have assigned $a =5 and your expression is $a > 4.
    Then you will get a true value. Now, if your expression is $a > 7, you will get a false value.
  • Less than or equal to (<=): This operator returns a true value if left side value is smaller or equal to the right side value. Otherwise it will return a Boolean false value. Say, $a =5. Now, you declare an expression like $a <= 8. Then the result will be true because 5 is less than 8. Again, your expression is $a <=5. Now result will be true because $a is equal to 5. Again, your expression is $a <= 4. This time your result will be false because 5 is greater than 4.
  • Greater than or equal to (>=) : The operation of this operator is reverse from the previous less than equal to operator(<=). If left side value is greater or equal from the right side value, the result will be Boolean true otherwise false. Say, $a = 10 and an expression is $a >= 5. So, you will get Boolean true because 10 is greater than 5. Now your expression is $a >= 10. This time you will also get true value. Again, your expression is $a >= 15. Then you will get a Boolean false because 10 never be greater or equal to 15.

Logical Operator

Logical operator also returns Boolean true or false value with the combination of two expressions. It is also a frequently used operator in PHP Language. The Logical operator is divided into four types.

  • AND operator (&&): This operator can be expressed as (&&) or string (and or AND). This operator returns a Boolean true if the left side expression and the right side expression of this operator return true value. The operation of AND operator is shown in the below truth table.
    Left Expression Right Expression Result
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    Here 0 = false and 1 = true. Left Expression represents the result of left expression of AND operator and Right Expression represents the result of right expression  of AND operator. So, you can easily understand from the truth table that if both  expressions of AND operator are true, you will get true value otherwise false value. You can use this logical operator to verify the username and the password of any application. If both the username and the password of any user are matched, the user can login to the application otherwise the user will be denied.

  • OR operator (||): This operator can be written as (||) or string (or and OR). This operator returns a Boolean true if any expression of this operator returns true value. The truth table of OR operator is given below…
    Left Expression Right Expression Result
    0 0 0
    0 1 1
    1 0 1
    1 1 1

    From the truth table, you can easily understand that if any side of OR operator gives true value, then the result will be true otherwise false. Say, you have developed an application to give points 10 to the US people and the UK people. So you can develop the application logic like if country equal to UK OR country equal to US, then give 10 points else give 5 points. This is a tiny example of OR operator.

  • Exclusive OR (XOR) operator: This operator returns true value if both side of this operator return the same logical value. The truth table of XOR operator is given below…
    Left Expression Right Expression Result
    0 0 1
    0 1 0
    1 0 0
    1 1 1
  • Logical NOT (!): The Logical NOT operator returns reverse logical value. If the logical value is true, it returns false and if the logical value is false, it returns true. Every PHP variable returns two values. Firstly, it returns assigned value and secondly it returns true if a value is assigned otherwise returns false. Say $a=5; If your expression is !$a, you will get false because there is a value in variable $a. Now you assign $a=NULL. If your expression is !$a, you will get Boolean true because there is no value assigned in variable $a.

Bitwise Operator

The Bitwise operator returns any numeric value. This operator works at bit level and rarely used in PHP programming. There are six kinds of Bitwise operator.

  • Bitwise and (&)
  • Bitwise or (|)
  • Bitwise xor (^)
  • Bitwise not (~)
  • Left shift (<<)
  • Right shift (>>)

You will get more details about Bitwise Operator in PHP manual page.

Concatenate Operator

This operator is used to add two string value. The concatenate operator is denoted as dot (.). This is a frequently used operator in PHP programming. An example of this operator is given below…

Concatenation codeSo, the output will be “My name is Tomas”. Here, two strings are concatenated with dot (.) operator.

PHP Operator and Expression are explained in this article and previous article. If you face any problem to understand PHP Operator and Expression, feel free to contact with from Contact us page. I’ll try my best to stay with you. PHP Conditional Statement, another important topic in PHP programming, will be explained in next article. Spend some times to study the article and make your idea clear about PHP Conditional Statement.

No Responses

Leave a Comment

Your email address will not be published. Required fields are marked *