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

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

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

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

Leave a Reply

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

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