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_change_key_case: Change Array Key Cases

    The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

    Install PHP on Operating System: Ubuntu, Windows, and macOS

    In this tutorial, you will learn how to install PHP on your Operating system such as Ubuntu, Windows, or macOS.…

    What Is PHP Used For? Meaning of PHP Programming Language

    When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…

    PHP array_count_values: How it Works with Examples

    Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…

    PHP error_log Function: How It Works with Examples

    Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…

    PHP htmlspecialchars Function: Prevent XSS in HTML Output

    A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

    PHP Escape Characters: How to Escape Special Characters

    Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…

    PHP Variable Basics: Everything You Need to Know

    Think about the PHP variable as little storage boxes holding your data, whether it is a number, a word, or…

    PHP array_key_first Function: How it Works with Examples

    The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

    PHP array_pop: Remove and Return the Last Element of an Array

    The array_pop function in PHP gives you a direct way to take the last element from an array. Understand the…

    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.