OPERATORS IN PHP
ARITHMETIC OPERATORS IN PHP

PHP Operators

  • All programming languages use operators.
  • Operators are pre-defined symbols that perform specific actions on objects called operands
  • Operators are assigned a precedence value indicating the order in which the operators are evaluated.

Classification of Operators

Operators are classified as follows:

  • Unary Operator – acts on only one operand in an expression
  • Binary Operator – has two operands and performs different arithmetic and logical operations
  • Conditional or Ternary Operator – has three operands and evaluates the second or third expression depending on the result of the first expression.

Arithmetic Operators In PHP

  • Arithmetic Operators are binary operators that work only on numeric operands.
  • If operands are non-numeric values such as strings, Booleans, nulls, or resources, they are converted to their numeric equivalents.

Relational OR COMPARISON Operators In PHP

  • Compares two operands and returns either a true or a false value
  • The operands can either be numbers or string values
Relational Operators In PHP

Increment and Decrement Operators In PHP

  • Operate only on variables
  • Cause a variable to change its value
  • The increment operators increase the value of the operand by one (+ +)
  • The decrement operators decrease the value of the operand by one (- -)
Increment and Decrement Operators In PHP

Logical Operators In PHP

  • Enable to combine two or more expressions in a condition
  • Evaluates the expression and returns a Boolean value of either true or false
  1. AND — &&
  2. OR — ||
  3. XOR
  4. NOT — !
And Operator In PHP
OR Operator In PHP
XOR Operator In PHP
Logical Operators In PHP

Assignment Operators In PHP

  • Enables to assign a value to a variable
  • The ‘=’ sign is the assignment operator
  • Operand on the left side of the assignment operator ‘=’ should be a variable
  • The assignment operation can be performed by:
    • Value – copies the value of expression 2 and assigns it to expression 1
    • Reference – assigns a reference of expression 2 to expression 1
  • The basic assignment operator can also be used as a shorthand operators (combined operator) in conjunction with arithmetic and string operators
  • The shorthand operators are +=, -=, *=, /=, %=, and .=
Assignment Operators In PHP

Conditional or Ternary Operators In PHP

  • An alternative to the if-else statement
  • Evaluates an expression for a true or false value and then executes one of the two statements depending upon the result of evaluation
  • Ternary (? 🙂 Operator
  • It is also known as a conditional operator
  • It simplifies complex conditions into one-line statements
  • It is considered as an alternative for the if…else statement
Conditional or Ternary Operators In PHP

Precedence of Operators In PHP

The order of precedence to evaluate the expression in PHP is:

  • Operators with higher precedence are evaluated first in an expression
  • Operator precedence can be overridden by using parenthesis. The expression within the parenthesis is evaluated first
  • If two operators of the same precedence are encountered, the expression will be evaluated from left to right
  • PHP follows the BODMAS rule for operator precedence
  • Example: 8 / 2 – 3 + 2 * 2
Precedence of Operators In PHP

Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    
    <?php
    
    
    for($i = 1; $i <= 10; $i++)
    {
        if($i == 5)
        {
//            continue; // skips the current iteration
            //break;
            exit;
        }
        echo $i . "<br>";
    }
    
    echo "Loop terminates";
    
//    // bodmas rule
//    $var1 = 8 / 2 - (3 + 2) * 2;
//             4    -    5 * 2
//                4  -     10
//                 -6
//    echo $var1;
    
    // 1st ()
    // 2nd / *
    // 3rd + -
    
    
//    $FirstBreak = "12pm";
//    $SecondBreak = "3pm";
//    
//    if( ($FirstBreak == "11pm") xor ($SecondBreak == "2pm") )
//    {
//        echo "its a break time";
//    }
//    else
//    {
//        echo "its not a break time";
//    }
    
//    $a = 10;
//    $a += 5; 
//    $a = $a + 5;
//    echo $a;
//    
//    $Age = 18;
//    $CheckAge = ($Age >= 18) ? "You are eligible to vote" : "You are not eligible to vote";
//    
//    echo $CheckAge;
    
//    $a = "10";
//    $b = "20";
//    
//    $a .= $b;
//    echo $a;
    
//    $a = 10;
//    $b = 20;
//    
//    if( !($a == $b) )
//    {
//        echo "True";
//    }
//    else
//    {
//        echo "False";
//    }
    
    ?>
    
</body>
</html>

Download Notes From the link given below.

https://www.mediafire.com/file/evjh1i1j04c8l83/OPERATORS+IN+PHP.pptx/file

No responses yet

Leave a Reply

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