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 Chriss Miller 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
Joined
Jan 5, 2003
Messages
235
Location
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];
}

 
ok but then how do i echo() the answers onto the screen or assign them a new variable of $side??

Martin

Computing help and info:

 
To assign the value returned by the funtion to a variable, I recommend that you use PHP's basic assignment operator.

You can either perform the assignment to the variable then echo the variable or echo the return from the function directly.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
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!!
 
<?php
myVar=myFunction();
echo myVar;
?>
 
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:

 
It's a typo.

In your function, you return an element of $possibilties. But the array is named $possibil[red]i[/red]ties

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
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