oppure passi la variabile contenente la stringa alla funzione htmlentities() prima di stamparla.

string htmlentities ( string $string [, int $quote_style = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )