For a page with one fixed language, write the language directly in the opening tag, such as <html lang="en">. If the page language varies at runtime, have PHP choose a supported language tag and print it on a single <html> element. In a PHP template, use if, elseif, else, and endif to conditionally output markup.
Use a fixed value when the page language does not change
PHP is not required just to add a lang attribute. If every version of the document uses the same language, set it in HTML:
<html lang="en">
The lang global attribute declares the language of an element’s content. Its value is a single BCP 47 language tag; an empty value means the language is unknown, and a value that is not valid under BCP 47 is invalid. Elements without their own lang or xml:lang inherit the language from a parent. See MDN’s documentation on the HTML lang attribute.
Choose the language in PHP when it varies
If the language depends on a route, locale setting, or translation system, select a tag from the languages your site supports and use it in the opening document element. For example:
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →#1 Best Overall
<?php
$language = $supportedLanguage; // A tag selected from the site's configured languages.
?>
<!doctype html>
<html lang="<?= htmlspecialchars($language, ENT_QUOTES, 'UTF-8') ?>">
Here, $supportedLanguage represents a value chosen by the application; it is not a built-in PHP variable. Keep language selection in the part of the application responsible for locale handling, then output the resulting tag. Escaping the value when inserting it into an HTML attribute helps prevent unsafe characters from being interpreted as markup. The PHP and HTML language references explain the template syntax and tag semantics, not a complete locale-selection or application-security design.
Put conditional markup inside PHP’s alternative syntax
When each language branch needs different template markup, PHP’s alternative if syntax lets you move in and out of PHP around the HTML:
Rank #2
<?php if ($language === 'fr'): ?>
<html lang="fr">
<?php elseif ($language === 'en-GB'): ?>
<html lang="en-GB">
<?php else: ?>
<html lang="en">
<?php endif; ?>
The colon begins each branch, and endif; closes the conditional. PHP also supports else and elseif in this form. Its manual cautions that alternative syntax and brace syntax cannot be mixed within the same control block. See PHP’s alternative syntax documentation.
Prefer one document root when only the tag value changes
The conditional example illustrates the syntax, but printing several possible opening <html> tags is usually unnecessary when only the language value changes. Selecting the tag first and printing one root element keeps the template simpler. Use conditional branches for markup only when the rendered structure genuinely differs.
What the lang attribute does—and does not do
The attribute describes the language of the content for an element. It does not translate text, choose which translation your application serves, hide other language versions, or change the browser’s language. Those are separate application behaviors. Choose the correct document language for the content being rendered; use the site’s routing or translation logic to decide what content to render.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




