Anchor

An anchor is an HTML element <a> used to create hyperlinks, allowing navigation to other pages, resources, or sections within the same page. When the href attribute references a fragment identifier, e.g. #id, the link jumps to a specific section of the current document.

Anchors are frequently generated programmatically when building HTML output, navigation menus, or documentation pages. Developers must be careful to escape user-supplied content used in anchor href attributes to prevent XSS vulnerabilities.

<?php

    // Generating an anchor safely in PHP
    $url   = 'https://www.php.net/';
    $label = 'PHP Manual';

    // htmlspecialchars prevents XSS
    echo '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '">'
        . htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
        . '</a>';

    // Fragment / in-page anchor
    echo '<a href="#section-2">Go to section 2</a>';

?>

Documentation

See also Nested Anchor Links using CSS.

Related : HyperText Markup Language (HTML), Cross Site Scripting (XSS), Universal Resource Locator (URL), parse_url(), http_build_query(), HTML Escaping, Link, Pound #