简体   繁体   中英

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; 7 characters long where the last 2 numbers are reserved for the cardnumber. So, with card 1-9 I add 6 numbers and otherwise I add 5 numbers to complete the range.

The echo fi gives the number 21. Still I get a cardnumber with a length of 8 characters instead of 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); $kaart2 = 10202 . str_pad($krtnum2, 2, '0', STR_PAD_LEFT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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