在if语句中使用有时未设置的变量时出现未定义的索引错误


Undefined Index Error when using variable in if statement that is sometimes not set

我使用这个if语句,但有时$_GET['av']没有设置。如果未设置,则会引发错误。有办法绕过这个吗?

if ($admin == 1 AND ($_GET['av'] == "0" OR !isset($_GET['av'])))

是的,只需将第二条语句放在第一条即可。

if ($admin == 1 AND (!isset($_GET['av']) OR $_GET['av'] == "0"))

这样,如果没有设置$_GET['av'],代码将永远不会引用它,if语句将在isset处停止,并且由于返回值为false,块将不会执行。

还可以查看Demorgan定律以及它如何适用于if语句。这可能有助于澄清更复杂语句的格式。