I have two values, i want to echo one of them on page refresh and rotate it.
If the two values are Hello and Hi.
Hello > Page refresh > Hi > Page refresh >Hello > Page Refresh > Hi
I tried shuffle
, rand
, mt_rand
but sometimes it just keep the same value instead of rotating to the next.
Thanks.
<?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();
}
its basically a page hit counter with logic on what to print depending on if the page hits are even or odd.
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