Tuesday 21 July 2015

PHP Operators

Like other programming languages, PHP includes set of operators that are used to perform operations between one or more values.
PHP has variety of operators based on the operations to be performed. Each operator has precedence, depends on which order an PHP expression will be executed. Sub expressions that hold operators with higher precedence, are having higher priority during execution.
In this article, we are going to see about various type of PHP operators in the order of their precedence.
php_operators

Auto increment/decrement operators in PHP

This operators can be categorized into two types. These are,
  1. pre increment/decrement. For example,
    echo ++i; // increment before print
    echo --i; // decrement before print
  2. post increment/decrement. For example,
    i++; // increment after print
    i--; // decrement after print
  3. Arithmetic Operators

    Addition (+), Subtraction (-), Division (/), Multiplication (*) and modulus (%) operators are coming under this category. Among them, (+,-) has lower precedence than rest of the arithmetic operators.

    PHP Comparison Operators

    Comparison operators are used in conditional expressions of PHP control flow statements like, if, else if, while and so on. For example,
    while(i<=10) {
     ...
    }
    In PHP, the following comparison operators are available.
    <, >, <=, >=, ==, === and !=
    Among them, the first four operators are having higher precedence than others.

    Logical Operators

    PHP includes the following list of logical operators.
    &&, ||, and, or, xor
    Logical operators represented by symbols and words, i.e. (or and ||) return same result, but vary with the precedence. The symbol representation has two level higher precedence than the word representation of logical operators. Because, assignment and combined assignment operators are having higher precedence than the word representation of logical operators.

    Assignment / combined assignment operators in PHP

    Assignment operators are used either to create a copy of PHP variable or to store some value to the new variable.
    Combined assignment operators are used to reduce the length of the expression and number of operands. For example,
    $i = $i + 1;
    //using combined arithmetic operator
    $i += 1;

    Other additional PHP Operators

    Apart from the above list of operators, PHP includes some additional list of operators. These are,
    • Concatenation operator - dot (.) is used to concatenate two or more strings, or to add HTML code into PHP print statements. For example,
      $user = "PHP Guest";
      echo "Hello" . $user . "<br/>";
    • Conditional operator - ( <condition> ? <if true> : <if false> ). For example,
      $output = $_SESSION["userName"]?$_SESSION["userName"]:"ANONYMOUS";
      echo $output;
    • Backticks - PHP supports to execute shell command, by enclosing it within two backticks (``) in a PHP printing statements. For example,
      $output = `ls -al`;
      echo $output;
    • Suppression operator - (@) is used to suppress any warning or error notice from displaying to the browser. For example,
      @split("-",date("d-m-Y");
      By using @ symbol infront of split function which is deprecated, we can suppress the PHP error message.

0 comments:

Post a Comment