Jump to content

Recommended Posts

I'm new to PHP, and scripting languages in general.  I'm writing a program for a low-level summer course in web application programming and I've gotten snagged.  The code is simply to generate a series of characters to simulate those found on a standard New Jersey auto license plate, in the form "AAA-11A" (three letters, a hyphen, two numbers and a final letter).

 

I assumed that this would be easy enough, but when I start my local Apache server and run the file in Firefox 3.6.6, the output is only correct part of the time.  Quite often, I'll notice that some of the characters are missing, and they're invariably letters.

 

I've posted the entire contents of this file below.

 

<?php

/* Program:  NJ_plate_generator_test.php
* Info:     Script to dynamically generate random license plate numbers according  
*           to the State of New Jersey format of three letters, a dash, two numbers  
*           and a final letter (ex. AAA-11A).
*/

  $rand_arr = array ();

  for($i=0; $i<=5; $i++)
  
  {

   if ($i < 3 || $i > 4)

   {

    $x = rand(1,26);

    switch ($x)

    {
     
     case 1:
      $rand_arr[$i] = 'A';
      break;

     case 2:
      $rand_arr[$i] = 'B';
      break;

     case 3:
      $rand_arr[$i] = 'C';
      break;

     case 4:
      $rand_arr[$i] = 'D';
      break;

     case 5:
      $rand_arr[$i] = 'E';
      break;

     case 6:
      $rand_arr[$i] = 'F';
      break;

     case 7:
      $rand_arr[$i] = 'G';
      break;

     case 8:
      $rand_arr[$i] = 'H';
      break;

     case 9:
      $rand_arr[$i] = 'I';
      break;

     case 10:
      $rand_arr[$i] = 'J';
      break;

     case 11:
      $rand_arr[$i] = 'K';
      break;

     case 12:
      $rand_arr[$$i] = 'L';
      break;

     case 13:
      $rand_arr[$i] = 'M';
      break;

     case 14:
      $rand_arr[$i] = 'N';
      break;

     case 15:
      $rand_arr[$i] = 'O';
      break;

     case 16:
      $rand_arr[$i] = 'P';
      break;

     case 17:
      $rand_arr[$$i] = 'Q';
      break;

     case 18:
      $rand_arr[$i] = 'R';
      break;

     case 19:
      $rand_arr[$i] = 'S';
      break;

     case 20:
      $rand_arr[$i] = 'T';
      break;

     case 21:
      $rand_arr[$i] = 'U';
      break;

     case 22:
      $rand_arr[$i] = 'V';
      break;

     case 23:
      $rand_arr[$i] = 'W';
      break;

     case 24:
      $rand_arr[$i] = 'X';
      break;

     case 25:
      $rand_arr[$i] = 'Y';
      break;

     case 26:
      $rand_arr[$i] = 'Z';
      break;

    }

   }

   else

   {

    $x = rand(1,10);

    switch ($x)

    {

     case 1:
      $rand_arr[$i] = '0';
      break;

     case 2:
      $rand_arr[$i] = '1';
      break;

     case 3:
      $rand_arr[$i] = '2';
      break;

     case 4:
      $rand_arr[$i] = '3';
      break;

     case 5:
      $rand_arr[$i] = '4';
      break;

     case 6:
      $rand_arr[$i] = '5';
      break;

     case 7:
      $rand_arr[$i] = '6';
      break;

     case 8:
      $rand_arr[$i] = '7';
      break;

     case 9:
      $rand_arr[$i] = '8';
      break;

     case 10:
      $rand_arr[$i] = '9';
      break;

    }

   }

  }


?>

<html>
<head>
<title>New Jersey License Plate Generator Test</title>
</head>

<body>

The six-character plate number should appear below.
<p>

<?php

  for($i=0; $i<=6; $i++)
  
  {

  echo "$rand_arr[$i]";

  if ($i == 2)

  {

  echo "-";

  }

  }

?>

</body>
</html>

 

Basically, while I want and sometimes get something in the form AAA-11A, sometimes I get AA-11A, AAA-11A, or even -11 if I'm terribly unlucky.  :P

 

I can't seem to figure out why this happens.  My experience tells me that the issue is sitting in plain view, but my experience also hasn't granted me the eyes to see it yet.

 

Also, in addition, if anyone could tell me how I could also store the complete output as a string type, I'd be grateful.  I'd assumed it'd involve use of the settype() function, or even initializing a string at the top and concatenating in the switch cases where I currently add to an array (thereby negating the array), but I've been so frazzled about getting the main problem fixed that I haven't touched this issue yet.

 

Any help would be immensely appreciated!

<?php

/* Program:  NJ_plate_generator_test.php
* Info:     Script to dynamically generate random license plate numbers according  
*           to the State of New Jersey format of three letters, a dash, two numbers  
*           and a final letter (ex. AAA-11A).
*/

  $rand_arr = array ();

  for($i=0; $i<=5; $i++)
  
  {

   if ($i < 3 || $i > 4)

   {

    $x = rand(1,26);

    switch ($x)

    {
     
     case 1:
      $rand_arr[$i] = 'A';
      break;

     case 2:
      $rand_arr[$i] = 'B';
      break;

     case 3:
      $rand_arr[$i] = 'C';
      break;

     case 4:
      $rand_arr[$i] = 'D';
      break;

     case 5:
      $rand_arr[$i] = 'E';
      break;

     case 6:
      $rand_arr[$i] = 'F';
      break;

     case 7:
      $rand_arr[$i] = 'G';
      break;

     case 8:
      $rand_arr[$i] = 'H';
      break;

     case 9:
      $rand_arr[$i] = 'I';
      break;

     case 10:
      $rand_arr[$i] = 'J';
      break;

     case 11:
      $rand_arr[$i] = 'K';
      break;

     case 12:
      $rand_arr[$i] = 'L';
      break;

     case 13:
      $rand_arr[$i] = 'M';
      break;

     case 14:
      $rand_arr[$i] = 'N';
      break;

     case 15:
      $rand_arr[$i] = 'O';
      break;

     case 16:
      $rand_arr[$i] = 'P';
      break;

     case 17:
      $rand_arr[$i] = 'Q';
      break;

     case 18:
      $rand_arr[$i] = 'R';
      break;

     case 19:
      $rand_arr[$i] = 'S';
      break;

     case 20:
      $rand_arr[$i] = 'T';
      break;

     case 21:
      $rand_arr[$i] = 'U';
      break;

     case 22:
      $rand_arr[$i] = 'V';
      break;

     case 23:
      $rand_arr[$i] = 'W';
      break;

     case 24:
      $rand_arr[$i] = 'X';
      break;

     case 25:
      $rand_arr[$i] = 'Y';
      break;

     case 26:
      $rand_arr[$i] = 'Z';
      break;

    }

   }

   else

   {

    $x = rand(1,10);

    switch ($x)

    {

     case 1:
      $rand_arr[$i] = '0';
      break;

     case 2:
      $rand_arr[$i] = '1';
      break;

     case 3:
      $rand_arr[$i] = '2';
      break;

     case 4:
      $rand_arr[$i] = '3';
      break;

     case 5:
      $rand_arr[$i] = '4';
      break;

     case 6:
      $rand_arr[$i] = '5';
      break;

     case 7:
      $rand_arr[$i] = '6';
      break;

     case 8:
      $rand_arr[$i] = '7';
      break;

     case 9:
      $rand_arr[$i] = '8';
      break;

     case 10:
      $rand_arr[$i] = '9';
      break;

    }

   }

  }


?>

<html>
<head>
<title>New Jersey License Plate Generator Test</title>
</head>

<body>

The six-character plate number should appear below.
<p>

<?php

  for($i=0; $i<=5; $i++)
  
  {

  echo "$rand_arr[$i]";

  if ($i == 2)

  {

  echo "-";

  }

  }

?>

</body>
</html>

 

You have acouple extra $'s in your first switch, also the loop at the bottom  should be $i<=5

function getRandom()
{
    $letters = range('A', 'Z');
    $digits = range(0, 9);

    return $letters[array_rand($letters)] . $letters[array_rand($letters)] . $letters[array_rand($letters)] . '-' .
           $digits[array_rand($digits)]  . $digits[array_rand($digits)]  . $letters[array_rand($letters)];
}

Alternatively, pretty much the same thing:

 

function getRandom()
{
    $options = array_merge(range('A', 'Z'), range(0, 9));

    return $options[rand(0,25)] . $options[rand(0,25)] . $options[rand(0,25)] . '-' .
           $options[rand(26,35)]  . $options[rand(0,25)]  . $options[rand(0,25)];
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.