Understanding the Increment and decrement operators in PHP

The increment and decrement are operators that can be used to increase or decrease the variable values which have a string or an integer as a data type in PHP. That means the increment and decrement operators are not working with objects, arrays, Booleans, and other data types.

    The used operators in PHP are the same in C style pre and post (increment and decrement).

    Anyway, the following table shows you a list of the increment and decrement operators that are working in PHP.

    The increment operators

    NameOperator PatternResult
    Pre Increment++$zTo increase the $z variable by 1 then return $z
    Post Increment$z++To return  the $z and then increase it by 1

    The decrement operators

    NameOperator PatternResult
    Pre Decrement–$zTo decrease the $z variable by 1 then return $z
    Post Decrement$z–To return  the $z and then decrease it by 1

    Anyway, there is another data type can work with only increment operator but it doesn’t effect of the exactly needed result. It is null (no value), when you use it with increment it will show you a result with 1.

    Let’s take each one in-depth with examples.

    Post Increment with Example

    The pre-increment increments the variable by 1 and then return the variable which means if you have 50 assigned to the $c variable then you added a pre increment $c++, it would increase the $c by 1 so the final result should be 51.

    The full example would be like the below.

    <?php 
       
      $c = 50;
      // the assigned value is 50
    
      $c++; // Return the previous value (50) 
      // => Increase the $c by 1  
    
      
      echo $c; //  => the output is 51
      
      echo $c++; // Return the previous value (51)
      // => Increase the $c by 1  
      
      echo $c; // the output is 52
    
    ?>

    If you didn’t understand it check the following figure to learn more about it.

    increment and decrement operators

    Let’s see what is happening with arithmetic operators.

    <?php 
      $x = 10;
      $y = 10;
      
      $z = $x++ + $y++;
      echo $z; // 20
    ?>

    The output of this example would be 20 and not 22 because this $z variable only returns the previous value of the $x and $y.

    Let’s see another example with subtraction.

    <?php 
      
      $x = 100;
      $y = 50;
      
      $z = $x++ - $y++;
      echo $z; // 50
    
    ?>

    Pre Increment with examples

    The pre increment to increase the variable by 1 and then returns the value so it is affecting in the current line. Check the following example.

    <?php 
      $c = 100;
      echo ++$c; // 101 
    ?>

    In the post increment it only return the previous value but here return the current value because it assigns the new value then return it in the same time.

    See another example.

    <?php 
       $b = 10;
       ++$b;
       echo $b; // 11
    ?>

    For arithmetic operators.

    <?php  
       $x = 50;
       $c = 100;
      
       echo ++$x + ++$c; // 152
    
       echo ++$x - ++$c; // -50
    ?>

    Pre Decrement with examples

    The pre decrement to decrease the value by 1 and then return the value. For example.

    <?php 
     
       $x = 5;
       echo --$x; // 4
    
    ?>

    For another example.

    <?php 
     
      $y = 20;
      --$y;
      echo $y; // 19
    
    ?>

    Use the pre decrement with arithmetic operators.

    <?php 
     
      $y = 55;
      $x = 20;
    
      echo   --$y + --$x; // 73
      echo   --$y - --$x; // 35
    ?>

    Post Decrement with Examples

    The post decrement to return the variable value and then decrease the variable by 1 for example.

    <?php 
     
       $x = 40;
       echo $x--; // 40
       echo $x; // 39
    
    ?>

    Use the post decrement with arithmetic operators.

    <?php 
       $x = 10;
       // => 10 + 9
       echo $x-- + $x--; // 19
       echo $x-- - $x--; // 
    ?>

    Use the PHP String Data Type with Decrement and Increment

    In this section the decrement and increment depending on the type juggling because it dynamically converting the string to numbers.

    Let’s see an example.

    <?php
      $x = "this is number 5";
      $x++;
      echo $x;
    ?>

    So the result would be like the following

    this is number 6

    For another example.

    <?php 
      $z = "this number 150 will not increase";
      echo ++$z; // 150
      echo $z;
    ?>

    Wrapping Up

    In this article you learned more about the decrement and the increment. Also you understood how it does work in the arithmetic operators.

    Thank you for reading, stay tuned for my next article.

    Similar Reads

    PHP filter_id Function: How to Retrieve PHP Filter IDs

    Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…

    How to Install PHP on a Raspberry Pi

    You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…

    PHP Class Destructor: How It Works with Examples

    A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…

    PHP OOP Programming: A Complete Tutorial

    A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

    PHP array_merge_recursive: Merge Arrays Deeply

    PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

    PHP Spread Operator: Understand How (…) Syntax Works

    Before the spread operator, PHP developers had to pass array items to a function using extra steps. They often had…

    PHP OOP Constructor: How It Works in a Class with Examples

    The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

    Class and Object in PHP: How They Work with a Real Example

    Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

    PHP array_intersect_ukey Function: How it Works with Examples

    The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

    PHP Type Juggling: How it Works Behind the Scenes

    PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…

    Previous Article

    PHP Comparison Operators Guide with Examples

    Next Article

    PHP Logical Operators | Understanding the 6 Logical Operators

    Write a Comment

    Leave a Comment

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


    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.