Home »
PHP »
PHP Programs
Using PHP Variable in HTML: Title, H3, and Anchor Text Example
By Shahnail Khan Last updated : December 11, 2023
Problem statement
$var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document.
Sample Output: (See in Output section)
PHP Tutorial
PHP, an acronym for Hypertext Preprocessor, is a widely-used open source general-purpose scripting language. It is a cross-platform, HTML embedded server-side scripting language and is especially suited for web development.
Go to the PHP Tutorial.
Prerequisites
To understand this solution better, you should have the basic knowledge of the following PHP topics:
Displaying the PHP variable ('PHP Tutorial') in the title section, h3 tag, and as an anchor text within an HTML document
In PHP, the variables are declared using the $ symbol followed by the variable name. Unlike other programming languages, PHP does not require explicit declaration of variable types. It can only contain letters, numbers, and underscores.
To display the PHP variable in the title section, we can insert the PHP script inside the <title> tag.
The syntax for embedding the PHP Script in the <title> section of the HTML document is:
<title>
<?php>
//Write PHP code here
?>
</title>
An anchor tag (<a>) in HTML is used to create hyperlinks. It allows you to link one web page to another or link to different resources like images, documents, etc.
You can insert the anchor tag in an HTML document like this:
<a href ="paste the link here"> add relevant text for the link </a>
We can embed the PHP script in the anchor tag as well. The syntax for the same is:
<a href ="paste the link here"> add relevant text for the link </a>
<?php
//PHP Code here
?>
PHP script for the given problem statement
In the below PHP code, we are displaying the PHP variable ('PHP Tutorial') in the title section, h3 tag, and as an anchor text within an HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $var; ?></title>
</head>
<body>
<?php $var = "PHP Tutorial"; ?>
<h3><?php echo $var; ?></h3>
<p>PHP, an acronym for Hypertext Preprocessor, is a widely-used open source general-purpose scripting language. It is a cross-platform, HTML embedded server-side scripting language and is especially suited for web development.</p>
<p>Go to the <a href="https://www.includehelp.com/php/">
<?php echo $var; ?></a>
</p>
</body>
</html>
Output
The output of the above code is:
Code Explanation
- As you already know <?php and ?> tags indicate the beginning and end of PHP code in the script.
- The $var variable is explicitly set to 'PHP Tutorial', and its value is displayed in the title, h3 tag, and anchor text within the HTML document.
- An anchor tag (<a>) is used to create a hyperlink that directs us to Includehelp's website. We have used a PHP variable (here, PHP Tutorial) as a text to display for this hyperlink.
More PHP Programs »