dongqiang2069 2014-07-17 17:22
浏览 14
已采纳

使用str_pad(PHP)递增数字[关闭]

I am creating an incrementing number starting with 1001. If the number goes 1001,1002,1003... when it reaches 10, will it be formatted like 1010 or will it be 10010? I need it to just go in order and be 1010 and when it reaches 100, 1100.

$prefix = "1"; // update the prefix here

    $number = 1;
    $number++;
    $unique = str_pad($number, 3, "0", STR_PAD_LEFT);
    $unique = $prefix . $unique;

print_r($unique);
  • 写回答

3条回答 默认 最新

  • donglijuan8227 2014-07-17 17:35
    关注

    When your count reaches 10, the number printed will be 1010. As described here, str_pad "Pads a string to a certain length with another string" You can create a test with the following:

    $prefix = "1"; // update the prefix here
    
    $number = 1;
    
    for ($number = 1; $number <= 100; $number++)
    {    
      $unique = str_pad($number, 3, "0", STR_PAD_LEFT);
      $unique = $prefix . $unique;    
      print($unique."
    ");
     }
    

    When your count reaches 100, the number printed will be 1100.

    However, if you were to go up to 1000, 11000 would be printed - str_pad apparently will not truncate the string to match the specified size.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?