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 array_shift: Remove the First Array Element with Examples

    The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

    PHP mail() Function: A Complete Guide

    PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…

    PHP sizeof: How to Count Elements in Arrays with Examples

    The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

    PHP array_key_last Function: How it Works with Examples

    The array_key_last gives you the last key in an array and works with numeric and associative arrays in PHP. It…

    PHP strlen Function: How It Works with Strings

    Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…

    PHP array_diff_assoc: How to Compare Arrays with Keys

    Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…

    PHP array_column: How to Extract Values from Arrays

    The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

    PHP Comparison Operators Guide with Examples

    PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…

    PHP MySQL ORDER BY: How to Sort Data in SQL?

    Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…

    PHP range Function: Create Arrays with Sequence Values

    The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…

    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.