From the course: Learning PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Incrementing and decrementing variables

Incrementing and decrementing variables - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Incrementing and decrementing variables

- [Instructor] Arithmetic operators allow you to enumerate as needed. Perhaps you want to count the number rows in the table or create a numbered list. To increment based on what you know at this point, you would use the first line of code here. A get a plus one. This takes the variable a, adds one to it and reassigns the new value back to a, and the same goes for decrementing. You could say b gets b minus one. But there's a slightly shorter way that you can do this using a ++ or -- symbol. So the second line you see on the screen here, a++, would increment a by one. Similarly, b-- would decrement b by one. And you don't have to use these in just assignments either. You can add them to echo statements as well. Just bear in mind a few things. If you use this shorthand anywhere, including echo statements, the variable will still change values. So you can't simply say echo a++ and assume a retains its previous value.…

Contents