简体   繁体   English

if-then-else语句中的php语法

[英]php syntax in if-then-else statement

I have a card program which selects cards from number 1 to 78. In the end I like to store the results in a database which requires a number; 我有一个纸牌程序,可以从1到78中选择纸牌。最后,我想将结果存储在需要数字的数据库中。 7 characters long where the last 2 numbers are reserved for the cardnumber. 长7个字符,最后2个数字保留给卡号。 So, with card 1-9 I add 6 numbers and otherwise I add 5 numbers to complete the range. 因此,使用卡1-9时,我将添加6个数字,否则将添加5个数字以完成范围。

The echo fi gives the number 21. Still I get a cardnumber with a length of 8 characters instead of 7. 回声fi给出数字21。仍然我得到的卡号长度为8个字符而不是7个字符。

echo $krtnum2;
if ($krtnum2 < 10) {
      echo $krtnum2;
      $kaart2 = 102020 . $krtnum2;
      echo "length is 1 digit, cardnumber is: ";
      echo $kaart2;
} else {
      echo $krtnum2;
      $kaart2 = 10202 . $krtnum2;
      echo "length is 2 digits, cardnumber is: ";
      echo $kaart2;
}

What is wrong about this code? 此代码有什么问题?

I assume that the most appropriate way to achieve your goal to use str_pad function like this: $kaart2 = 10202 . str_pad($krtnum2, 2, '0', STR_PAD_LEFT); 我假设使用str_pad 函数是实现目标的最合适方法,例如: $kaart2 = 10202 . str_pad($krtnum2, 2, '0', STR_PAD_LEFT); $kaart2 = 10202 . str_pad($krtnum2, 2, '0', STR_PAD_LEFT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM