Rand - shooting random blanks

Given these arrays, and echoes, must rand somehow have an associated number to bind with like the second alphanumeric example below?

$lets= array("A", "B", "C", "D", "E", "F", "G");
echo $lets[rand(0,7)];  // brings blank results here and there

$letters= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); 
$numbs= array("1", "2", "3", "4", "5", "6", "7", "8", "9");
echo $letters[rand(0,25)]; 
echo $numbs[rand(0,9)]; both of these together, always seems to succeed

I’m not sure what you are doing but it should not process any POST variables unless the correct answer was provided.

If you post the source where this is happening, I may be able to help you out better.

I think the problem is register_globals are off and the form action is set to another page.

Well you could the following:

if (isset($_POST['answer']) && $_POST['answer'] === 'your_answer_key_here') {
  //Validate the rest of the form, other POST variables etc...
else { //the response did not match the correct answer
  //display some error here...

Assigning individual varaibles since there are 20-25 of them and growing and each one is listed followed by its orderly answer, to check against:

$a=“How many colors are in the Sitepoint logo?”;
$aA= “2”; // challenge question requests numerical
$b = “How much is 2 + 3?”
$bA = “5”; // challenge question requests numerical

These are actually lengthier sentences though. Up to 60-70 characters.

Being accesssed by:

Im assuming is better than include_once() for this purpose, since there may be changeable data on the file, don’t know at this point.

These random variables will call pre-written challenge questions that go in a form as a security measure before being processed.

ex: $a=“How many colors are in the Sitepoint logo?”

That gives the error and processes anyway. Always displaying:

the response did not match the correct answer

$lets= array(“A”, “B”, “C”, “D”, “E”, “F”, “G”);
echo $lets[rand(0,7)];

That works correctly. It should be 0, 6 though, as the highest index is 6 (remember that arrays start from 0)

You can use the range function to get an array of letters/numbers

$letters = range('a', 'z');
$numbers = range(1, 9);

class Quiz
    protected $question, $answer;

    function __construct ( $question, $answer ) {}
    function __toString () { return $question; }

    function checkAnswer ( $submitted ) {}

$quizes = array();

// ...Pull Questions and Answers from XML
// Put ean Question and Answer par into a Quiz object
// $quizes[] = new Quiz( $xml_question, $xml_answer );

// Randomize Quizes...
shuffle( $quizes );

// Display...
foreach ( $quizes as $quiz ) {
    print $quiz;

// More code...

At least that is partly how I might go about it…

In order to get it to represent an actual variable you would do it like so:

echo "${$ranvar}";

But, what is your ultimate goal in this situation? I believe you may be approaching it incorrectly.

Thanks Cerium,

Then, what I am trying to do is convert each letter to stand for a variable.

$ranvar= $letters[rand(0,25)];
$convertvar = ‘$’.$ranvar;

Which will give an output of say “$b”

But the ‘$’ is maintaining its character value, and not transferring to an actual variable. Ideas on how to make it?

I suggest you setup a XML file to store these.

Here’s a quick example to show you how much easier it would be:

    <q>How many colors are in the Sitepoint logo?</q>
    <answer>Blue Orange</answer>

    <q>How much is 2 + 3?</q>

You can use simpleXML to access it SimpleXML: http://us.php.net/manual/en/book.simplexml.php

…in addition, I don’t think php will process a second time until the submit button is hit. (apologies for the double post – I could not edit the previous)

Any suggestions for validating the challenge question?

I’m finding that if you want to validate the question against the answer, this is not so straightforward with register_globals turned off. That is, if you want to validate before the other POSTS on the form are sent on to another page for sanitizing/insert.

Can validating be done with non-cookie sessions?

You can change the action to be on the same page, if everything was validated correctly, redirect the user to a new page.

Why don’t you place the question in an array and then randomly print one out?


$questions  = array(
'How many colors are in the Sitepoint logo?',
'How much is 2 + 3?',
'Are you a developer?'
//get the size of the array
//subtract one off since arrays begin from 0
$size = sizeof($questions) - 1;

echo $questions[rand(0, $size)];

It might be better to an XML file to store the questions or in a database to make your life easier.

For this purpose I would most likely go with the XML approach.