Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function returns a letter

Status
Not open for further replies.

martinb7

Programmer
Jan 5, 2003
235
GB
Hi, i was wondering how you could make a function that returns a letter e.g. a or b or c etc because my website has a scoreboard system where members get put randomly on side a or b or c when they register.

any ideass??

thanx


Martin

Computing help and info:

 
Assuming your question could be better phrased as, "How would you write a function that randomly returns a single letter?"

Code:
<?php

function random_letter ($start = 'a', $finish = 'd')
{
	return chr (rand (ord ($start), ord ($finish)));
}

?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Code:
function randLet() {
  $possibilities=array('a','b','c');
  $size=count($possibilities) - 1;
  $index=rand(0,$size);

  return $possibilties[$index];
}

 
You asked how to assign the value returned from a function to a variable. I provided a link to the PHP online manual page on assignment operators. Did you follow the link?



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Exactly the same way you assign a variable from anything else. The variable goes on the left-hand side of the assignment operator and the function reference goes on the right-hand side.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
yea i understand that but if i use skiflyer's example, it doesnt display anything on the page, why not??

Code:
<?php
function randLet() {
  $possibilities=array('a','b','c');
  $size=count($possibilities) - 1;
  $index=rand(0,$size);

  return $possibilties[$index];
}

$myVar=randLet();
echo $myVar;
?>

Martin

Computing help and info:

 
I must have put a decimal point in the wrong place or something. S***, I always do that, I always mess up some mundane detail.

Sorry about that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top