2

Could someone explain me the following?

$a="";

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

echo $a; 
// returns muh
iivannov
  • 4,341
  • 1
  • 18
  • 24
PeMa
  • 1,559
  • 18
  • 44

1 Answers1

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; //     
iivannov
  • 4,341
  • 1
  • 18
  • 24