Home > Backend Development > PHP Tutorial > PHP implements ID auto-increment and specifies the number of digits

PHP implements ID auto-increment and specifies the number of digits

不言
Release: 2023-03-24 13:04:02
Original
4644 people have browsed it

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[&#39;msg&#39;] = &#39;ID大于7位,不符合要求&#39;;           
   return self::alertJson($this->_json);        
   }    
   }
   }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template