$string = 'This string contains & and < and > characters.'; $encoded_string = htmlchars($string); echo $encoded_string; // Output: This string contains & and < and > characters.
$name = $_POST['name']; $safe_name = htmlchars($name); echo "Hello $safe_name!";In this example, the htmlchars function is used to sanitize user input from a form before it is displayed on the web page. This helps to prevent cross-site scripting (XSS) attacks where malicious code can be injected into the web page through user input. Package Library: htmlchars function is a built-in PHP function and does not require any external package or library.