0

My config.php contains:

$a1="title";
$b1="text";
$a2="title2";
$b2="text2";

and I have 1.php which includes config.php.

I need to include ($a1 and $b1) or ($a and $b) randomly.

How can I do this? :)

Thank you.

2
  • 3
    Its really hard to understand exactly what you are trying to do... Could you please try and explain? Commented May 20, 2012 at 17:12
  • use two arrays. Store the possible values as array elements in the arrays $a and $b. Then pick a random index $i and use it as $a[$i] and $b[$i] Commented May 20, 2012 at 17:14

1 Answer 1

1

If this data is related, and you need a random set, store it in an array:

$sets = array(
    array(
        'title' => 'Some title',
        'text'  => 'Some text here about the title'
    ),
    array(
        'title' => 'Some other title',
        'text'  => 'Some other text here about the title'
    )
);

With this array, we have two indexes that we can choose from:

$sets[0]; // Title: Some title, Text: Some text here about the title
$sets[1]; // Title: Some other title, Text: Some other text here about the title

If we want one of those, randomly, we could do the following:

$index = array_rand( $sets, 1 );

This will select either 0, or 1. With more entries in our array, the potential for this number to be larger also increases. We can then use it to grab one of our sets of data:

$dataSet = $sets[ $index ];

And then display the output:

echo $dataSet['title'];
echo $dataSet['text'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.