Jump to content

[SOLVED] Random letter?


ERuiz

Recommended Posts

Hello guys and gals,

 

Look at the following code:

 

if ($carrier == "Chautauqua Airlines") {
$reg_number = rand (100, 999);
$reg_number = "N".$reg_number."SK";
}

 

As you can see, if $carrier equals Chautauqua Airlines, then it will make a random number from 100-999 and build the following result: N###SK

 

What I want to know is how can I also make this code so that instead of always using SK, it would randomly select between using SK or JQ or RP?

 

Thanks for any help, as always.

 

Regards,

 

ERuiz

Link to comment
https://forums.phpfreaks.com/topic/75012-solved-random-letter/
Share on other sites

<?php

$random_letter_ucase = chr(rand(ord("A"), ord("Z"))); 


if ($carrier == "Chautauqua Airlines") {
$reg_number = rand (100, 999);
$reg_number =  ($random_letter_ucase+1),$reg_number,($random_letter_ucase+1);

///should output something like  FG###SD
}?>

Link to comment
https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379299
Share on other sites

darkfreaks remember shuffle will shuffle an array BUT whats does it return!!!

 

shuffle($array);

not

$randarray=shuffle($array); as it returns a boolean ;)

 

EDIT: oh if you must have different random items then shuffle works well

ie

<?php
$array= array("SK","JQ","RP");
shuffle($array);
echo $array[0]; echo $regnumber; echo $array[1]; ?>

which i think darkfreaks  was aiming for

Link to comment
https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379315
Share on other sites

<?php
$array= array("SK","JQ","RP");
$randarray= array_rand($array);
$randarray2= array_rand($array);
echo $array[$randarray]; sprintf($regnumber); echo $array[$randarray2]; ?>

 

 

 

Thanks! That did the trick! Also MANY thanks to darkfreaks, who also helped out. Thanks to both you guys.

 

ERuiz

Link to comment
https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379319
Share on other sites

in darkfreaks defence i have seen LOTS of people attempt shuffle like that..

 

also please be aware.. that the code you use will allow results like SK###SK and JQ###JQ where as the following code will not..

 

EDIT: oh if you must have different random items then shuffle works well

ie

<?php
$array= array("SK","JQ","RP");
shuffle($array);
echo $array[0]; echo $regnumber; echo $array[1]; ?>

which i think darkfreaks  was aiming for

Link to comment
https://forums.phpfreaks.com/topic/75012-solved-random-letter/#findComment-379324
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.