1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Constants and Operator Types

Discussion in 'PHP' started by MinalS, Oct 22, 2015.

  1. Constants



    The name allocated to a value is known as constant. The value is fixed and cannot be modified by the user. It is case sensitive. The name of the constant starts with a letter, following numbers, letters, or underscores. It is case sensitive. The following are examples of various PHP constants.
    Code:
    define( "NEW", "new value");
    define( "OLD", "old value");
    
    The define() function is useful for accessing values of the constant. There is no need for $ sign for a constant. User can read values to be retrieved from the name of the constant. The constant() function is used for accessing constants value. Only constant values are obtained.

    Distinguish between constant and variables
    1. The constants are defined and accessed in the program regardless of the scoping rules
    2. The dollar sign ( $ ) is not added before the constant but the variable contains a dollar sign.
    3. User cannot modify the constants in PHP
    4. The define() function is used for defining the constant values
    Various PHP constants

    There are several PHP predefined constants used for the execution of scripts. There are five constants which can be used appropriately.
    1. _FILE_ : The filename and full path of the file. The name of the file is returned.
    2. _LINE_: The line number of the file is returned
    3. _CLASS_: The class name declared by the user is returned
    4. _METHOD_: The class method name is described
    5. _FUNCTION_: The name of the function is defined

    Operators in PHP



    The operators in PHP are the one which are used for performing the operations on the operands. There are various types of PHP operators supported.
    1. Arithmetic operators
    2. Logical operators
    3. Comparison operators
    4. Assignment operators
    5. Conditional operators
    We shall explore about all these operators in detail.

    1. Arithmetic operators

    The arithmetic operators perform operations like calculating numeric values, property and function calls, etc. The two values can be added using + operator and subtracted using – operator.

    The various arithmetic operators provided in PHP are as mentioned below:
    1. Addition: It is used for adding two operands. The + operator is used.
    2. Subtract: The second operand is subtracted from the first. The – operator is used.
    3. Multiplication: Used for multiplying both the operands. It is represented as *.
    4. Division: The numerator is divided by the denominator
    5. Modulus: The remainder after the integer division is performed
    6. Increment operator: Used for increasing the integer value by 1. It is defined as ++
    7. Decrement operator: Used to decrease the integer value by 1. It is represented by --.
    Example to demonstrate the use of arithmetic operators
    Code:
    
    <?php
    echo ( 4 + 2 ). "\n";
    echo ( 3 – 2). "\n";
    echo ( 10 % 2). "\n";
    ?>
    
    2. Logical operators

    The logical operators work on Boolean values. They combine logical values and create a Boolean value. The various logical operators supported by PHP are mentioned below:
    1. And: It is true when both the arguments have value as true.
    2. Or: It is true when either of the arguments have value as true
    3. Xor: The value is true if either of the arguments have value as true
    4. Not: It holds the value true if the single argument has the value as false
    5. &&: The value is true if both the arguments have value as true
    6. ||: If either of the value is true, the result is true
    Example:

    Code:
    <?php
        $x = true && false;
        var_dump ( &x);
        $y = true || false;
        var_dump ( &y );
        $z = false || false;
        var_dump ( &z);
    ?>
    
    
    3. Comparison operators

    The comparison operators are used on strings and numbers. The list of comparison operators is as mentioned below:
    1. == : The value is true if the arguments are equal
    2. >: If the left argument is greater than the right, the result is true
    3. <: If the right argument is greater than the right, the result is true
    4. ! = : If the two arguments are not equal, the result is true
    5. < = If the left argument is less than or equal to the right, the value is true
    6. >= : If the right argument is greater than or equal to the right, the value is true
    Example:
    Code:
    <?php
        $x = 400;
        $z = "400";
        var_dump ( $x == $y );
    ?>
    
    4. Assignment operators

    The assignment operators used in PHP are as mentioned below:
    1. = : The operator is used to assign the value from right operands to the left side operand
    2. -=: The subtract AND operator is used to the right operand from the left operand
    3. * =: The multiplication of right with the left operand is done and the value is added to the left operand
    4. /= : It divides the left operand and the value is assigned to the left operand
    5. %=: The modulus of using two operands and values is assigned to the left operand
    Example:
    Code:
    <? php
        $p = 5;
        $p += 4;
        $q = 6;
        $q -= 2;
    ?>
    
    5. Conditional operators

    In conditional operators, the expression is calculated to a Boolean value and the further execution is dependent on the result of the calculation. It is denoted as ?:.

    If condition is True? Then value X: otherwise value Y.

    Operator Precedence

    The precedence of the operators is dependent on the expressions. The evaluation of the expression is useful. The operators in PHP has precedence. The operators with higher precedence is calculated first.

    The list of operators with their precedence are as mentioned below:

    1. Unary: The unary operators used in PHP are !, ++, --. The value is evaluated from right to left
    2. Multiplicative: The operators *, /, 5. The values are calculated from left to right
    3. Additive: The operators + and -. The values are calculated from left to right
    4. Relational: The operators < < = > > =. Evaluation from left to right
    5. Equality: The operators == , !=. The values are calculated from left to right
    6. Logical AND: The && operator is used. Calculated from left to right
    7. Logical OR: The || operator is used. Calculated from left to right
    8. Conditional: The ?: operator is used. Associativity from right to left
    9. Assignment: The operator =, +=, -=, *=, /=, %=. The associativity from right to left
     

Share This Page