Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果字符串是字母和数字的组合,如何在字符串中添加数字?_Php_String_Operators - Fatal编程技术网

Php 如果字符串是字母和数字的组合,如何在字符串中添加数字?

Php 如果字符串是字母和数字的组合,如何在字符串中添加数字?,php,string,operators,Php,String,Operators,我想递增IDT100这个字符串(IDT100,IDT101,…IDT1000等),它是动态的 我试过下面的代码 <?php $cart_billno = "IDT100"; echo $cart_billno += 1 ;//....1st try echo $cart_billno = $cart_billno + 1 ;//...2 nd try ?> 回答我得到id1 我的代码有什么问题 改用预增量: $cart_billno = "IDT100"; echo ++$

我想递增
IDT100
这个字符串(
IDT100,IDT101,…IDT1000等),它是动态的

我试过下面的代码

<?php

$cart_billno = "IDT100";

echo $cart_billno += 1 ;//....1st try

echo $cart_billno = $cart_billno + 1 ;//...2 nd try

?>
回答我得到id
1


我的代码有什么问题

改用预增量:

$cart_billno = "IDT100";
echo ++$cart_billno; // IDT101
或使用普通for循环:

$cart_billno = 'IDT';
for($i = 100; $i <= 1000; $i++) {
    echo $cart_billno.$i . '<br/>';
}
$cart_billno='IDT';

对于($i=100;$i使用预增量:

$cart_billno = "IDT100";
echo ++$cart_billno; // IDT101
或使用普通for循环:

$cart_billno = 'IDT';
for($i = 100; $i <= 1000; $i++) {
    echo $cart_billno.$i . '<br/>';
}
$cart_billno='IDT';
对于($i=100;$i
$cart\u billno=“IDT”;
$i=99;
而(++$i<1001){
echo$cart_billno.$i;
}
将IDT100打印到IDT1000; $i=99; 而(++$i<1001){ echo$cart_billno.$i; }

将IDT100打印到IDT1000

如果需要同时将值增加到1以上,请使用以下方法:

$cart_billno = "IDT100";
echo substr($cart_billno, 0, 3) , (substr($cart_billno, 3) + 99); // IDT199

在我看来,这是一个比将字母数字字符串增加1更干净的解决方案。

如果需要同时将值增加到1以上,请使用以下方法:

$cart_billno = "IDT100";
echo substr($cart_billno, 0, 3) , (substr($cart_billno, 3) + 99); // IDT199

在我看来,这比增加一个字母数字字符串更干净。

string++
是一个很好的功能。@Prashant发生的事情是
IDT100
=
0
,您正在添加
(int)1
,所以它等于
1
@Ghost:真的谢谢你,但是我的代码有什么问题..我为这么一件小事而受挫hour@Prashant实际上,这个行为在手册中有说明。PHP如何处理this@Prashant这在我提供给您的链接
中有说明,该值由字符串的初始部分给出对于有效的数字数据,这将是使用的值。否则,值将为0(零)。有效的数字数据是可选符号,后跟一个或多个数字(可选包含小数点),后跟可选指数。指数是一个“e”或“e”,后跟一个或多个数字。
string++
是一个不错的功能。@Prashant发生的情况是,
IDT100
=
0
,您正在添加
(int)1
,所以它等于
1
@Ghost:真的谢谢你,但是我的代码有什么问题..我为这么一件小事而受挫hour@Prashant实际上,这个行为在手册中有说明。PHP如何处理this@Prashant这在我提供给您的链接
中有说明,该值由字符串的初始部分给出对于有效的数字数据,这将是使用的值。否则,值将为0(零)。有效的数字数据是可选符号,后跟一个或多个数字(可选包含小数点),后跟一个可选指数。指数是一个“e”或“e”,后跟一个或多个数字。
未获取..你能解释吗?未获取..你能解释吗?