Skip to content
PHP Logo

How can PHP developers ensure valid XHTML when including variables in URLs?

To ensure valid XHTML when including variables in URLs, PHP developers can use the `urlencode()` function to properly encode the variables before appending them to the URL. This function will ensure that special characters are encoded correctly, preventing any potential validation errors.

php
$variable = "example value";
$encoded_variable = urlencode($variable);
$url = "https://www.example.com/page.php?var=" . $encoded_variable;
echo $url;