w1coding.comAll tutorials

PHP Operators

PHP lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Basics
  5. Operators
  6. Control
  7. Loops
  8. Variables
  9. Functions
  10. Arrays
  11. Superglobal
  12. File
  13. Loops
  14. Date
  15. Loops
  16. Strings
  17. Error
  18. Class
  19. File
  20. Include
  21. Sessions
  22. Mysql
  23. Cookies
  24. Forms
  25. Cookies
  26. Regular
  27. Authenthication
  28. Fileupload
  29. Mvc
  30. Responsive

Operators in PHP

Operators are used to perform operations on variables and values. PHP supports various types of operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations:

      
        <?php
          $x = 10;
          $y = 5;
          echo $x + $y; // Addition
          echo $x - $y; // Subtraction
          echo $x * $y; // Multiplication
          echo $x / $y; // Division
        ?>
      
    

Comparison Operators

Comparison operators are used to compare values:

      
        <?php
          $x = 10;
          $y = 5;
          echo $x == $y; // Equal to
          echo $x != $y; // Not equal to
          echo $x > $y; // Greater than
        ?>
      
    

Activity

Try It Yourself!

Write a PHP script that uses both arithmetic and comparison operators to calculate and compare two numbers.

Quiz

Quick Quiz

  1. What is the difference between == and !=?
  2. What operator would you use to multiply two numbers?

Answers: