0

我很难使用 isset() 函数。究竟我如何告诉 php,只有当数据被分配给我的时候,比如说 $_SESSION['msg']; 变量,然后才激活它并使用它......到目前为止,我收到了很多通知:未定义的索引并且这段代码也没有这样做:

if (isset($_SESSION['monster']) &
isset($_SESSION['spol']) &  
isset($_SESSION['combat']) &
isset($_SESSION['turns']) &  
isset($_SESSION['zmaga']) &
isset($_SESSION['zguba']) &
isset($_SESSION['cekini']) &
isset($_SESSION['post']) )

{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
}

数据应该从重定向的先前站点分配给它,但该站点并不总是激活,所以我需要一个替代方案。这就是我问这个的原因。

编辑完整代码:

session_start();

include 'razno.php';
include 'save.php';
include 'stats.php';

$igralec_ime = $_SESSION['username'];
$monster = prikazi_borba($igralec_ime);
$poskodbe = prikazi_stat('curhp', $igralec_ime);

if (prikazi_stat('curhp', $igralec_ime) == 0)
{
$moznost = 'Tvoje zdravje je resno ogroženo, vrni se domov!';
}

else if (isset($_SESSION['monster']) &&
isset($_SESSION['spol']) &&  
isset($_SESSION['combat']) &&
isset($_SESSION['turns']) &&  
isset($_SESSION['zmaga']) &&
isset($_SESSION['zguba']) &&
isset($_SESSION['cekini']) &&
isset($_SESSION['post']) )
{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];

}

update_save($igralec_ime, 'gozd', $monster);

include '../html/gozd.html';
?>
4

2 回答 2

0

你应该看看PHP中的逻辑运算符。在许多编程语言(包括 PHP)&中是“按位与”运算符(不合逻辑)。由于您需要逻辑运算符,因此您需要使用&&or and。也看看其他 PHP 运算符,只是为了熟悉它们并避免这样的问题。

此外,如果您在 if/then 块之外使用这些值,您应该考虑您的语句为假的情况并为这些变量分配一些默认值。

于 2013-08-06T23:13:56.743 回答
0

您需要将 & 替换为 and:

if (isset($_SESSION['monster']) and isset($_SESSION['spol']) and isset($_SESSION['combat']) and isset($_SESSION['turns']) and isset($_SESSION['zmaga']) and isset($_SESSION['zguba']) &
isset($_SESSION['cekini']) and isset($_SESSION['post']) )

{
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
}
于 2013-08-06T22:56:29.640 回答