句子之间自动切换
Auto switch between sentences
我希望我的 joomla 网站能够自动更改它将向用户回显的句子,所以我写了 3 个不同的句子:
$sentence1 = "Everything okay?";
$sentence2 = "Have a good day";
$sentence3 = "What are you doing today?";
我希望它能在句子之间切换,所以我知道我不能只把 $sentence1
放在 echo
中,但我不知道怎么写。我有这样的回声线:
echo "Hey {user->name}." . "<br />" . $sentence1
顺便说一下,{user->name}
来自 Joomla 自己的 "codes",所以效果很好 :)
在我看来,你可以将你的 3 个句子或 3 个字符串放在一个数组中,然后你可以在循环时打印每个句子。示例:
$array = array(sentence1, sentence2, sentence3 , ... , sentence n);
for ($index = 0; $index < sizeof($array); $index++) {
echo 'This is sentence ' + index ':' + $array[index];
}
您可以使用随机函数,例如 mt_rand()
:
$sentence1 = "Everything okay?";
$sentence2 = "Have a good day";
$sentence3 = "What are you doing today?";
$nb = mt_rand(1, 3); // Gets a random number from 1 to 3
$sentence_shown = ${'sentence' . $nb}; // Equals $sentence1, $sentence2 or $sentence3
echo "Hey {user->name}." . "<br />" . $sentence_shown;
或者更好的是,将三个字符串放在一个数组中:
$sentences = array();
$sentences[] = "Everything okay?";
$sentences[] = "Have a good day";
$sentences[] = "What are you doing today?";
$nb = mt_rand(0, 2); // Gets a random number from 0 to 2
$sentence_shown = $sentences[$nb];
echo "Hey {user->name}." . "<br />" . $sentence_shown;
随机问候语:
$sentence[1] = "Everything okay?";
$sentence[2] = "Have a good day";
$sentence[3] = "What are you doing today?";
echo "Hey {user->name}." . "<br />" . $sentence[rand(1,3)]
我希望我的 joomla 网站能够自动更改它将向用户回显的句子,所以我写了 3 个不同的句子:
$sentence1 = "Everything okay?";
$sentence2 = "Have a good day";
$sentence3 = "What are you doing today?";
我希望它能在句子之间切换,所以我知道我不能只把 $sentence1
放在 echo
中,但我不知道怎么写。我有这样的回声线:
echo "Hey {user->name}." . "<br />" . $sentence1
顺便说一下,{user->name}
来自 Joomla 自己的 "codes",所以效果很好 :)
在我看来,你可以将你的 3 个句子或 3 个字符串放在一个数组中,然后你可以在循环时打印每个句子。示例:
$array = array(sentence1, sentence2, sentence3 , ... , sentence n);
for ($index = 0; $index < sizeof($array); $index++) {
echo 'This is sentence ' + index ':' + $array[index];
}
您可以使用随机函数,例如 mt_rand()
:
$sentence1 = "Everything okay?";
$sentence2 = "Have a good day";
$sentence3 = "What are you doing today?";
$nb = mt_rand(1, 3); // Gets a random number from 1 to 3
$sentence_shown = ${'sentence' . $nb}; // Equals $sentence1, $sentence2 or $sentence3
echo "Hey {user->name}." . "<br />" . $sentence_shown;
或者更好的是,将三个字符串放在一个数组中:
$sentences = array();
$sentences[] = "Everything okay?";
$sentences[] = "Have a good day";
$sentences[] = "What are you doing today?";
$nb = mt_rand(0, 2); // Gets a random number from 0 to 2
$sentence_shown = $sentences[$nb];
echo "Hey {user->name}." . "<br />" . $sentence_shown;
随机问候语:
$sentence[1] = "Everything okay?";
$sentence[2] = "Have a good day";
$sentence[3] = "What are you doing today?";
echo "Hey {user->name}." . "<br />" . $sentence[rand(1,3)]