Php 使用递增运算符后,输出小于预期值
我创建了一个类abc和一个静态变量,还有两个名为setsize的函数,其中我设置了一个值,另一个函数我创建了一个getsize来获取getsize函数中的值,当我调用一个函数时,它的输出必须是101,但它的输出是100为什么Php 使用递增运算符后,输出小于预期值,php,increment,post-increment,pre-increment,Php,Increment,Post Increment,Pre Increment,我创建了一个类abc和一个静态变量,还有两个名为setsize的函数,其中我设置了一个值,另一个函数我创建了一个getsize来获取getsize函数中的值,当我调用一个函数时,它的输出必须是101,但它的输出是100为什么 <?php Class abc { // create a class public static $a; static function getsize() { make a function return self::$a++;
<?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
您所需要做的就是使用预增量来实现您想要的结果。这是因为您正在使用带有回音的++
代码:()
在更简单的演示中:()
输出:
100
101
---
101
$hundy = 100;
echo $hundy++;
echo "\n$hundy";
echo "\n---\n";
$hundy = 100;
echo ++$hundy;
100
101
---
101