XF 2.1 Random phrase in PHP

Ozzy47

Well-known member
In my PHP file I have this bit of code:

PHP:
            if ($spamcheck == '')
            {
                return $this->error(\XF::phrase('did_not_complete_the_captcha_verification_properly'));
            }

This works as intended. Now what I would like to do is use a random phrase from a list, how would I acheive that?
 
PHP:
if ($spamcheck == '')
{
    $phrases = [
        'did_not_complete_the_captcha_verification_properly',
        'another_phrase',
        'some_other_phrase',
        'yet_another_phrase'
    ];
    $key = array_rand($phrases);
    return $this->error(\XF::phrase($phrases[$key]));
}
 
PHP:
if ($spamcheck == '')
{
    $phrases = [
        'did_not_complete_the_captcha_verification_properly',
        'another_phrase',
        'some_other_phrase',
        'yet_another_phrase'
    ];
    $key = array_rand($phrases);
    return $this->error(\XF::phrase($phrases[$key]));
}

Perfect, I see now what I was missing when I tried, and I also did not get the return right.
 
Back
Top Bottom