0

I have a PHP echo value that displays a value from a MySQL database like this:

<?php echo($row_WADAHTG_TechProps["EmpType"]); ?>

I need to construct an if/else with this value but am getting a syntax error no matter how I try to arrange this:

<?php
if (echo($row_WADAHTG_TechProps["EmpType"]) = "6001") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

4 Answers 4

2

try that:

 <?php
      if ($row_WADAHTG_TechProps["EmpType"] == "6001") {
             echo "Have a good day!";
      } else {
             echo "Have a good night!";
      }
 ?>
Sign up to request clarification or add additional context in comments.

4 Comments

+1, but why aren't you using the strict comparison operator (i.e ===)?
@JackWilliam === is used for checking the equality as well as type.
6001 can maybe a integer or string . so it will be true in both. but with === it will just accept same type.
You're right, but I presumed he was expecting it as a string. It depends on the OP's app.
1
if ( $row_WADAHTG_TechProps["EmpType"] == "6001") {
   echo "Have a good day!";
 } else {
   echo "Have a good night!";
 }

Careful with the single '=' sign in the if statement as that is an assignment.

Comments

1

what about

<?php
    if ($row_WADAHTG_TechProps["EmpType"] == "6001") {
        echo "Have a good day!";
    } else {
        echo "Have a good night!";
    }
?>

you have to compare a value with ==or === (if you also want to compare the type)

Comments

1

I use shorthand ternary operators for if/else conditions

($row_WADAHTG_TechProps["EmpType"] === "6001" ? echo "Yes" : echo "No");

Notice I'm strictly comparing $row_WADAHTG_TechProps["EmpType"] to "6001" as strings, therefore if either of them are integers, the condition will echo "No".

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.