search

Home  >  Q&A  >  body text

php - generate a 4 digit number

For the verification code, the code below generates a 5-digit number. I want it to be 4 digits. what should I do.

// generate random numbers
$sayilar = '';
for ($x = 15; $x <= 95; $x += 20) {
    $sayilar .= ($sayi = rand(0, 9));
    imagechar($image, rand(3, 5), $x, rand(2, 14), $sayi, $textcolor);
}

P粉709644700P粉709644700281 days ago430

reply all(1)I'll reply

  • P粉317679342

    P粉3176793422024-02-26 13:15:39

    Your loop definition is complex because you loaded a lot of unnecessary logic into it. Modification/maintenance will be clearer and easier:

    $sayilar = '';
    $lpad = 15;
    $offset = 20;
    for ($x = 0; $x < 4; $x) {
        $sayilar .= ($sayi = rand(0, 9));
        $xpos = $x * $offset $lpad;
        imagechar($image, rand(3, 5), $xpos, rand(2, 14), $sayi, $textcolor);
    }

    reply
    0
  • Cancelreply