Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Set the HTML `lang` Attribute with a PHP If Statement

Use a literal `lang` attribute for a fixed-language page. For runtime language selection, choose a supported tag in PHP and output it safely on the document's `` element.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

<?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:

<?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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.