PHP 会话变量性能

发布于 2024-11-17 02:47:50 字数 943 浏览 4 评论 0原文

我想知道下面的代码是否是 PHP 中会话变量最有效的使用方式。

<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
  $count = 0;
  $randomArray = array();
  $sql="SELECT youtubeurl FROM Foodlist";
  $result=mysql_query($sql);
  while($row=mysql_fetch_array($result)){
       array_push($randomArray,$row['youtubeurl']);
  }
  shuffle($randomArray);
  $_SESSION['randomArray'] = $randomArray;
  $_SESSION['count'] = $count; 
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
  $_SESSION['count'] = 0;
  $randomArray = $_SESSION['randomArray'];
  shuffle($randomArray);
  $_SESSION['randomArray'] = $randomArray;
} else{
  $randomArray = $_SESSION['randomArray'];
  $count = $_SESSION['count'];
  echo $randomArray[$count];
  $_SESSION['count']++;
}
?>

如果我做得正确的话,代码的意图应该是显而易见的。但基本上存在一个循环一次的字符串列表(randomArray)。每次完整通过后,列表都会被打乱。每个元素在任何单个元素打印两次之前都会打印一次。

那么,这是最有效的方法吗?

I was wondering if the code below was the most efficient use of session variables in PHP.

<?
session_start();
if (!isset($_SESSION['count']) || !isset($_SESSION['randomArray'])) {
  $count = 0;
  $randomArray = array();
  $sql="SELECT youtubeurl FROM Foodlist";
  $result=mysql_query($sql);
  while($row=mysql_fetch_array($result)){
       array_push($randomArray,$row['youtubeurl']);
  }
  shuffle($randomArray);
  $_SESSION['randomArray'] = $randomArray;
  $_SESSION['count'] = $count; 
} elseif ($_SESSION['count'] >= sizeof($_SESSION['randomArray'])){
  $_SESSION['count'] = 0;
  $randomArray = $_SESSION['randomArray'];
  shuffle($randomArray);
  $_SESSION['randomArray'] = $randomArray;
} else{
  $randomArray = $_SESSION['randomArray'];
  $count = $_SESSION['count'];
  echo $randomArray[$count];
  $_SESSION['count']++;
}
?>

The intent of the code should be obvious, if I did it correctly. But basically there exists an list(randomArray) of strings which is looped through once. After each complete pass, the list is shuffled. Every element prints out once before any single element is printed out twice.

So, is this the most efficient way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-11-24 02:47:50

我将其简化如下:

if (empty($_SESSION['randomList'])) {
    $result = mysql_query("SELECT youtubeurl FROM Foodlist"); // or die(mysql_error())
    while ($row = mysql_fetch_assoc($result)) {
       $_SESSION['randomList'][] = $row['youtubeurl'];
    }
    shuffle($_SESSION['randomList']);
}

echo array_shift($_SESSION['randomList']);

I'd simplify this as follows:

if (empty($_SESSION['randomList'])) {
    $result = mysql_query("SELECT youtubeurl FROM Foodlist"); // or die(mysql_error())
    while ($row = mysql_fetch_assoc($result)) {
       $_SESSION['randomList'][] = $row['youtubeurl'];
    }
    shuffle($_SESSION['randomList']);
}

echo array_shift($_SESSION['randomList']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文