0

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.

3
  • Can you explain the logic behind your condition? Like what's the case for "Admin" and for "Inactive"? Commented Mar 27, 2016 at 5:38
  • 1
    Your code is very difficult to follow in such a long block... Commented Mar 27, 2016 at 5:43
  • See Adam Azad's answer for a much better way to do this. Separate your logical decision making from building the email body, or it becomes very hard to see what is going on with your code. Commented Mar 27, 2016 at 5:55

2 Answers 2

2

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:";
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly, thank you. Still, I like Tim's inline approach, saves me from defining more variables.
@Armitage2k, you remove the variables and move the (($action == "change_access" && $access == "User") ? "Admin" : "User") to inside the variable. :)
0

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> ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.