English 中文(简体)
PHP [重复] 中前缀为零的编号
原标题:Numbering with prefix of zero in PHP [duplicate]
  • 时间:2012-05-24 05:41:09
  •  标签:
  • php

能否在PHP中以零点表示数字?

例如,例如

<?php

$i = 0001;
$i++;
echo $i;

?>

而当它打印出来, 我希望它像。

0002

有可能吗?

最佳回答

, 这是可能的。 您可以使用 < a href=" http:// php. net/ manual/ en/ opident.str-pad. php" rel = "nofollow" >str_pad () 的方法来做到这一点。 基本上, 这种方法在达到所需的长度之前会添加给定值 。

这方面的一个基本例子是:

echo str_pad($i, 4, "0", STR_PAD_LEFT); 

  • 4 represents the output length
  • "0" represents the string used to pad, till the length is achieved.

http://codepad.org/uWJfnICi" rel=“notfollow” >Demo 示例:

<?php
$i = 0001;
$i++;
echo str_pad($i, 4, "0", STR_PAD_LEFT);
问题回答
$i = 1;
$i++;
printf("%04d", $i); // 0002
  • printf - output a formatted string
  • %04d - echo a 4 digit number, pad with 0 s

试这个

echo str_pad($i, 4,"0",STR_PAD_LEFT);

这里读到以下文件:http://php.net/manual/en/form.str-pad.php

$input = 0001;
echo str_pad(++$input, 4, "0", STR_PAD_LEFT); 




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签