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 Logical Operators | Understanding the 6 Logical Operators

    The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…

    PHP filter_list(): List Available Filters

    PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

    PHP array_pop: Remove Last Element from Array Easily

    The array_pop function in PHP. Such a small function, yet so handy when you have to work with the last…

    PHP cURL: HTTP Requests and Responses in PHP

    In PHP, there are plenty of times when you need to connect with other servers or APIs. That's where the…

    PHP require_once and require

    Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…

    PHP foreach Loop: How to Access Keys & Values in Arrays

    PHP foreach loop came to help work with array data in a simple way. Arrays and objects grew more common…

    Parameters and Arguments in PHP: What’s the Difference?

    If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…

    PHP Hello World: Write Your First Program with Code Example

    You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…

    PHP Filters: How to Validate and Sanitize User Inputs

    User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

    PHP array_rand() Function: Usage & Examples

    The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…

    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.