PHP - Arithmetic Operations


  • Addition (+)
    Addition is performed with the "+" symbol.
    Ex:
    $total = 5 + 6;
    Here 5 add to 6 and assign to the total variable 
     
  • Subtract (-)
    Subtract is performed with the "-" symbol. 
    Ex:
    $answer = 8 - 6; 
      
  • Multiplication (*)
    Multiplication is performed with the "*" symbol. 
    Ex:
    $answer = 5 * 6; 
     
  • Division (/)
    Division is performed with the "/" symbol. 
    Ex:
    $answer = 6 / 3; 
     
  • Modulus(%)
    Modulus is performed with the "%" symbol. 
    Ex:
    $answer = 6 % 3; 
     
  • Increment (++)
    Increment is performed with the "++" symbol. 
    Ex:
    $answer++; 
    This increase answer by 1. 

     echo ++$answer; -> This statement increase 1 before displaying the answer.
     echo $answer++; -> This statement displays the value and then increase the answer by 1
     Ex:
       $ans = 10;
    .
       echo $ans++; // This displays 10. 
       echo $ans; // This displays 11. 
      
     echo ++$ans; // This displays 12. 
      
  • Decrement (--)
    Decrement is performed with the "--" symbol. 
    Ex:
    $answer--; 
    This decrease answer by 1. 

     echo --$answer; // This statement decrease 1 before displaying the answer.
     echo $answer--; // This statement displays the value and then decrease the answer by 1
     Ex: .
       $ans = 10;
    .
       echo $ans--; // This displays 10. 
       echo $ans; // This displays 9. 
      
     echo --$ans; // This displays 8. 
     
  • Compound Operations
    $ans += 6 // This executed as $ans = $ans + 6;
    $ans -= 6 // This executed as $ans = $ans - 6;
    $ans /= 6 // This executed as $ans = $ans / 6;
    $ans *= 6 // This executed as $ans = $ans * 6;
    $ans %= 6 // This executed as $ans = $ans % 6; 


No comments:

Post a Comment