PHP字符串中if语句的多个条件


PHP multiple conditions for if statement in string

我正在处理一封电子邮件,一旦我的数据库中的帐户访问/状态发生更改,就会发送该电子邮件。然而,我似乎无法让下面这个简单的IF语句发挥作用。我在AND和&,但是在这里找不到问题。

$email_message = "
<h1>User Account Update</h1><hr>
User <b>".$_SESSION['myusername']."</b> has just made changes to the following user account:<br>
<br>
Date: ".date("Y-m-d H:i:s", strtotime('+13 hours'))." (UTC+8)<br>
User: ".$user."<br>
<b>Action: ".str_replace("_"," ",$action)."</b><br>
<br>
Updated Access: ".if($action == "change_access" AND $access == "User") { echo "Admin"; } else { echo "User"; }."<br>
Updated Status: ".if($action == "change_status" AND $status == "Active") { echo "Inactive"; } else { echo "Active"; }."<br>
<hr>
If you suspect suspicious activity, you can suspend the account by following the link below:
";

if语句出现在字符串中时,有什么特殊语法吗?

Updated Access: ".if($action == "change_access" AND $access == "User") { echo "Admin"; } else { echo "User"; }."<br>
Updated Status: ".if($action == "change_status" AND $status == "Active") { echo "Inactive"; } else { echo "Active"; }."<br>

编辑:关于逻辑:每当特权用户(管理员帐户)更改另一用户的帐户状态或访问权限时,都会触发电子邮件警报。用户ACCESS为"Admin"或"User",用户STATUS为"Active"或"Inactive"。消息告诉我受影响的帐户及其这两个变量的先前和当前状态。

使用三进制来确定变量值

$userType = (($action == "change_access" && $access == "User") ? "Admin" : "User");
$newStatus = (($action == "change_status" && $status == "Active") ? "Inactive" : "Active");

然后在$email_message 中打印

$email_message = "
<h1>User Account Update</h1><hr>
User <b>".$_SESSION['myusername']."</b> has just made changes to the following user account:<br>
<br>
Date: ".date("Y-m-d H:i:s", strtotime('+13 hours'))." (UTC+8)<br>
User: ".$user."<br>
<b>Action: ".str_replace("_"," ",$action)."</b><br>
<br>
Updated Access: ".$userType."<br>
Updated Status: ".$newStatus."<br>
<hr>
If you suspect suspicious activity, you can suspend the account by following the link below:";

您还在内联中使用块if语句,并将结果回显到输出,而不是将它们添加到变量中。

如果必须内联If语句,请尝试

"Access: ".($action == "change_access" && $access == "User") ? "Admin" : "User"."<br> ...