我有两个值,我想在页面刷新时回显其中一个并旋转它。
如果这两个值是 Hello 和 Hi。
你好>页面刷新>你好>页面刷新>你好>页面刷新>你好
我试过shuffle
, rand
,mt_rand
但有时它只是保持相同的值而不是旋转到下一个。
谢谢。
<?php
session_start();
if (!isset($_SESSION["count"])) $_SESSION["count"] = 0;
$_SESSION["hits"]++;
echo ($_SESSION["hits"]%2 == 1?"Hi":"Hello");
// or with functions
if ($_SESSION["hits"]%2 ==1){
my_func_1();
} else {
my_func_2();
}
它基本上是一个页面命中计数器,其逻辑取决于页面命中是偶数还是奇数。
To rotate the string, something random is definitively not what you are seeking.
First to have to decide the scope of the rotation. Based on the distributed page from the server ? (odd pages hays hi) or based on the user that si visiting you (so the rotation is always visible to you, ever if other users have also the rotation)
I guess you want based on user:
So you have to link the rotation to the user that visit you, that can be managed through a cookie or through a PHP session
set the cookie and based on the value, say hello or hi
// before sending headers
$say = isset($_COOKIE['say'])?$_COOKIE['say']:1;
setCookie('say', $say==1?2:1);
if ($say == 1)
echo "hello";
else
echo "hi";
You can do the same thing with a PHP session
That gives you an idea