Armitage2k
Armitage2k

Reputation: 1274

PHP multiple conditions for if statement in string

I am working on an email message which will be sent out once account access / status has been changed in my database. However, I cannot seem to get this simple IF statement below to work. I played around with the syntax by switching between AND and &&, but cant find the problem here.

$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:
";

Is there any special syntax when the if statement happens in a string?

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>

EDIT: In regards to the logic: Everytime when a privileged user (the admin account) changes the account status or access right of another user, an email alert is triggered. User ACCESS is either "Admin" or "User" and user STATUS is "Active" or "Inactive". The message tells me the affected account and its prior and current state of those two variables.

Upvotes: 0

Views: 95

Answers (2)

Tim Ogilvy
Tim Ogilvy

Reputation: 1973

You are also using block if statements inline, and echoing the result to output instead of adding them to the variable.

If you must inline your if statements, try

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

Upvotes: 0

Adam Azad
Adam Azad

Reputation: 11297

Use ternary to determine the variables values

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

And, then print those in $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:";

Upvotes: 2

Related Questions