2

Could someone explain me the following?

$a="";

$a="" ? "" : "muh";

echo $a; 
// returns muh
2
  • Blank string evaluates to false. Commented Dec 9, 2017 at 8:50
  • Ok, that's the answer. Thanks! Commented Dec 9, 2017 at 9:33

1 Answer 1

4

It looks you are trying to use Comparison operator ==, but instead you are using an Assignment operator =

Your code is trying to assign $a the result of the expression "" ? "" : "muh". An empty string is evaluated as false and $a is assgined the value of muh.

Let's put some parentheses to make it more obvious:

//$a equals (if empty string then "" else "muh")
$a = ("" ? "" : "muh");

echo $a; // muh


//$a equals (if $a is equal to empty string then "" else muh)
$a = ($a == "" ? "" : "muh"); 

echo $a; //     
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.