Search code examples
phpoperatorsdouble-quotes

Operators execute withing double quote when there is no space


I am not sure this has been asked before.

I had old PHP code to grab some result from the database. I recently notice that doesn't work as expected. When I debug the code I found the issue, However, I still don'tbknow the reason behind it.

Let's say I have a code like follows,

$text1 = "A";
echo "BBB<$text1";  // print BBB
echo "<br/><br/>";
echo "BBB < $text1"; // print BBB < A

I am pretty sure that I miss some PHP basic concepts. But still I couldn't find it.

When I used the < within the double quote with space and without space I am getting two different results. Can anyone explain me how does that happen ?


Solution

  • To display correctly the first string you should use htmlentities() function:

    $text1 = "A";
    echo htmlentities("BBB<$text1"); // print BBB<A
    echo "<br/><br/>";
    echo "BBB < $text1";
    
    
    // BBB<A</br> is interpretted as a html tag