PHP - 如何返回HTML特殊字符代码?

I'm working on function like this one:

function foo() {
   return '[ = ]';
}

But this returns:

[ @ ]

And I need:

[ = ]

Any ideas?

You could either call htmlspecialchars on your string before you output it, or you could simply replace the & in your string with a &.

Either one will make a browser show = instead of @.

Your function is returning the correct string.

When you view it in an HTML document, it is interpreted as an entity.

My advice would be to encode the response when you display it, eg

$foo = foo();
echo htmlspecialchars($foo, ENT_QUOTES, 'UTF-8');

Changing your function's return value will probably cause issues later on when you may not want the encoded string.

or use:

return urlencode('[ = ]');