Home » Tutorials » PHP » Operators

Operators

Somebody bring in the math squad. Relax, how complicated can standard arithmetic operators be? In PHP there are 6 groups of operators: arithmetic, assignment, increment/decrement, comparison, logical, and array operators. We will just deal with the first 3 for now. Quick note: We are going to assume from now on that the code is inside the <?php ?> tags.

Arithmetic Operators

Example
    $a = 3;
    $b = 7;
    print $a + $b . "<br/>"; //Addition with the " . <br/>" being a line break
    echo $a - $b . "<br/>"; //Subtraction
    echo $a * $b . "<br/>"; //Multiplication
    echo $a / $b . "<br/>"; //Division
    echo $a % $b . "<br/>"; //Remainder aka Modulus
Result 10
-4
21
0.42857142857143
3

Nothing special here, they are pretty much standard across most languages.

Assignment Operators

You have already used one of these! See, you’re a genius and you didn’t even know it. The PHP assignment operators always have an = somewhere in the statement. The rest of the assignment operators are more like shortcuts. You can use them by throwing the assignment operators in front of the =. Let’s take a look.

Example
    $a = 3;
    $a += $a;
    echo $a;
    echo "<br/>";
    $b = 7;
    $b *= $b;
    echo $b;
Result 6
49

You’re smart, and you can figure out how to use the rest of them. As you can see with the variable $a, we basically add it to itself, which gives us 6. With $b, we multiple it by itself, which gives us 49. It’s literally just as simple as that. If you don’t want to use them, you don’t have too. Just have fun writing out the full $a = $a + $a; or $b = $b * $b;.

Increment and Decrement Operators

Four possible outcomes occur from increments and decrements. Notice the placement of the increment or decrement operator around the variable.

Example
    $a = 3;
    $b = 3;
    $c = 7;
    $d = 7;
    echo ++$a . "<br/>"; //adding 1 to $a before we echo (where the  . "<br/>" is a line break)
    echo $b++ . "<br/>"; //adding 1 to $b after we echo
    echo --$c . "<br/>"; //subtracting 1 from $c before we echo
    echo $d-- . "<br/>";//subtracting 1 from $d after we echo
Result 4
3
6
7

If you have ever messed with these guys before, you know what is going on here. If not, just understand that the position of the increment or decrement operator tells us when the action on the variable happens. In the first example, echo ++$a; increments the variable before we echo it out. However, in the second example, echo $b++;, we don’t increment the variable until after we echo it to the screen.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Operators". After Hours Programming. Accessed on October 25, 2024. https://www.afterhoursprogramming.com/tutorial/php/operators/.

  • Stewart, Suzy. "Operators". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/php/operators/. Accessed 25 October, 2024.

  • Stewart, Suzy. Operators. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/php/operators/.



2 thoughts on “Operators”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.