1

I have written the below code:

<?php
$text = 'class&section';
echo $text;
?>

But upon executing its giving me class§ion on my browser. Unable to understand why &section is displaying as §ion. Can any one help me?

  • Because [`&sect`](https://en.wikipedia.org/wiki/Section_sign) is an HTML entity. Use `htmlspecialchars` on your string to encode each individual character first. – Jeto Sep 22 '20 at 03:29
  • Does this answer your question? [php url string converts "&section=" to "§ion", which does not yield cURL response](https://stackoverflow.com/questions/39970170/php-url-string-converts-section-to-ion-which-does-not-yield-curl-respons) – Lukas Sep 22 '20 at 07:43

1 Answers1

2

&sect is a HTML entity, so you must use htmlspecialchars or htmlentities before outputting this to the browser. For the difference between these 2 functions, please see this answer.

Your code could be rewritten to the following:

<?php
$text = 'class&section';
echo htmlspecialchars($text);
?>
llui85
  • 177
  • 2
  • 14