The content of this article is about PHP's implementation of ID auto-increment and specifying the number of digits. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it
In the development process We often encounter such folding requirements:
ID value is self-increasing and the length is 7 digits. If there are not enough, use 0 to fill in front.
Special implementation:
mysql
id char(7) not null default '0000000';
php set public method
/** * @param $id * @return string|void */ protected function isNumAuto($id) { if (strlen($id) > 7) { $this->_json['msg'] = 'ID大于7位,不符合要求'; return self::alertJson($this->_json); } if (strlen($id) < 7) { $newid = sprintf("%07d", intval($id) + 1); if (strlen($newid) <= 7) { return $newid; } else { $this->_json['msg'] = 'ID大于7位,不符合要求'; return self::alertJson($this->_json); } } }
Related recommendations:
PHP implementation of WeChat web page login authorization development
PHP implementation in redis common usage scenarios
Analysis of multi-dimensional array sorting algorithm implemented in PHP
The above is the detailed content of PHP implements ID auto-increment and specifies the number of digits. For more information, please follow other related articles on the PHP Chinese website!