-1

I create a class abc and static variable and two function having name setsize in which I set a value and another function I create a getsize to get value in getsize function I increment a value when I call a function its output must be 101 but it output is 100 why

 <?php

Class abc {   // create a class 
  public static $a;

  static function getsize() {  make a function
    return self::$a++;   //increment a static variable
  }
  static function setsize($n) {
    self::$a=$n;  // set size of static variable
  }
}
abc::setsize(100);  // set value of static variable
echo abc::getsize();  //call getsize function output is 100 but it must be 
101 
yivi
  • 42,438
  • 18
  • 116
  • 138

1 Answers1

0

All you need to do is use pre-incrementation to achieve your desired result. This is because you are using ++ with an echo. http://php.net/manual/en/language.operators.increment.php

Code: (Demo)

Class abc{   // create a class 
    public static $a;

    static function getsize() {
        return ++self::$a;   //increment a static variable
    }
    static function setsize($n) {
        self::$a = $n;  // set size of static variable
    }
}
abc::setsize(100);  // set value of static variable
echo abc::getsize();  //call getsize function output is 100 but it must be 101
// output: 101

In a simpler demonstration: (Demo)

$hundy = 100;
echo $hundy++;
echo "\n$hundy";
echo "\n---\n";
$hundy = 100;
echo ++$hundy;

Output:

100
101
---
101
mickmackusa
  • 43,625
  • 12
  • 83
  • 136