Sunday, November 15, 2026 New tutorials published every day
Home
Tutorials
Core PHP PHP Course JavaScript React & Next.js Python WordPress Artificial Intelligence
Free Tools
JSON Formatter Base64 Encoder / Decoder Password Generator Text to QR Code Generator Word Counter Case Converter Age Calculator Color Converter Number to Words All Free Tools
Projects
PHP Projects — all projects
Students
Webflow Course Course Registration Download Syllabus Verify Certificate Student Leaderboard
Resources
WordPress Plugins Search tutorials About CodingCush Contact
More
Login
General

How to Use PHP to Add Title, Meta Tags, and Keywords for SEO

Use PHP to add title tags, meta descriptions and keywords to every page automatically, with static variables, dynamic database values and Open Graph tags.

You can add title tags, meta descriptions and meta keywords to every page of your website automatically with PHP by storing them in variables — or better, in your database — and printing them inside the <head> section of your page. This gives every page a unique, search-friendly title and description without editing hundreds of HTML files by hand. In this guide you will learn the static method, the dynamic database method, and the Open Graph tags that make your pages look great when shared.

What Are Meta Tags and Why Do They Matter?

Meta tags are small pieces of HTML code that live inside the <head> section of a page. Visitors do not see them on the page itself, but search engines read them to understand what the page is about, and browsers and social platforms use them to present your page nicely. The three tags that matter most are the title tag — the clickable headline in Google results; the meta description — the short paragraph under that headline; and meta keywords — a list of related words that most search engines ignore today, though a few smaller ones still read them.

Of the three, the title tag carries the most weight. It is one of the strongest on-page SEO signals, and it decides whether a searcher clicks your result or a competitor's. A good title is 50 to 60 characters long, puts the main keyword near the start, is unique on every page, and reads naturally to a human. The meta description, ideally 150 to 160 characters, works like a tiny advertisement: it rarely changes your ranking directly, but a clear, honest description earns more clicks.

Why PHP Makes Meta Tags Easy

PHP runs on the server before the page is sent to the browser, so it can build the head section fresh for every request. Imagine a blog with 100 posts. Writing a separate title and description into 100 static files is slow and error-prone. With PHP, the post title and summary are already in your database — you simply print them into the tags. One small template then gives every page on your site correct, unique SEO tags automatically.

Method 1: Static Meta Tags With PHP Variables

The simplest approach is to set three variables at the top of the page, then echo them inside the head. Here is the pattern using a small fruit shop as an example:

<?php
$page_title = "Buy Fresh Organic Apples Online | FruitStore India";
$meta_description = "Order fresh organic apples online in India. Fast delivery, best prices, and 100% natural fruit. Shop now at FruitStore.";
$meta_keywords = "buy apples online, organic apples India, fresh fruit delivery";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo htmlspecialchars($page_title); ?></title>
<meta name="description" c>
<meta name="keywords" c>
</head>
<body>
<h1>Welcome to FruitStore</h1>
</body>
</html>

Notice the htmlspecialchars() around every printed variable. It converts special characters into safe HTML, so a title containing quotes or an ampersand cannot break your page or open a security hole. Make it a habit every single time you print a variable into HTML. This static method is perfect for small sites where each page is its own PHP file.

PHP meta tags and title tag for SEO shown in a browser window
PHP prints the title tag and meta description into the head section of every page automatically.

Method 2: Dynamic Meta Tags From a Database

The real power appears when the values come from MySQL. Store a title, description and keywords column with each page or post, fetch the row for the page being viewed, and print it the same way:

<?php
// Connect to the database
$conn = new mysqli("localhost", "username", "password", "mydb");

// Get the page ID from the URL - intval() keeps it safe
$page_id = intval($_GET['id']);

// Fetch the page details
$result = $conn->query("SELECT title, description, keywords FROM pages WHERE id = $page_id");
$page = $result->fetch_assoc();

// Fall back to defaults if a field is empty
$page_title = $page['title'] ?? "Default Page Title";
$meta_description = $page['description'] ?? "Default description for this page.";
$meta_keywords = $page['keywords'] ?? "default, keywords, here";
?>

Then use the exact same head block as Method 1. Two details matter here. First, intval() forces the ID to be a number, which blocks a whole class of attacks through the URL. Second, the ?? fallback defaults guarantee that a page never shows an empty title, even if someone forgot to fill the fields in. With this setup, adding a new page with perfect meta tags takes seconds — you only type the content once, in the database.

Bonus: Open Graph Tags for Social Sharing

When someone shares your page on Facebook, WhatsApp, X or LinkedIn, those platforms look for Open Graph tags to build the preview card. Without them, the share often shows a random image and a dull snippet. Add these with the same PHP variables:

<meta property="og:title" c>
<meta property="og:description" c>
<meta property="og:type" c>
<meta property="og:image" c>

The og:image should be a full URL to an attractive image, ideally 1200 by 630 pixels. Because your title and description are already variables, these four lines cost you nothing and make every share look professional.

Common Mistakes to Avoid

The most common mistake is using the same title on every page — Google then cannot tell your pages apart, and rankings suffer. Stuffing titles with repeated keywords also backfires; write for the reader first. Keep descriptions honest: a description that promises what the page does not deliver raises your bounce rate. Finally, remember that the keywords meta tag is mostly ignored by Google today, so spend your effort on the title and description instead. To understand which numbers actually tell you whether this work is paying off, read our complete guide to SEO metrics, backlinks, keywords and rankings.

If this database-driven pattern interests you, it is exactly how real PHP sites are built. Our free course shows the full journey — watch the PHP and MySQL website course introduction to see the complete project you will build.

A Practical Workflow for Site-Wide Meta Tags

Here is the workflow most PHP developers follow. Create one shared header.php file that contains the whole head section and prints the three variables. At the top of every page, set $page_title, $meta_description and $meta_keywords before including that header. For pages that come from the database — blog posts, products, course lessons — fill the variables from the database row instead, and use the static values only as a fallback. When you redesign or rename a page, you then update its tags in exactly one place. This small discipline keeps a growing website fully covered: no page is ever left sharing the homepage's title by accident, which is the duplicate-title problem Google notices first.

Frequently Asked Questions

Where do meta tags go in a PHP page?

Inside the head section of the HTML, before the closing head tag. The PHP code that sets the variables must run before that section is printed.

Does Google still use the meta keywords tag?

No, Google has ignored it for many years. It does no harm to include it, but your ranking effort should go into the title tag and good content.

How long should a title tag and description be?

Aim for 50 to 60 characters for the title and 150 to 160 characters for the meta description, so they display fully in search results.

Why should I use htmlspecialchars() when printing meta tags?

It escapes characters like quotes and angle brackets, so data from your database cannot break the HTML or inject unwanted code into the page.

Can one PHP template serve meta tags for my whole site?

Yes. Put the head section in a shared header file, set the variables before including it on each page, and every page gets its own correct tags.

Conclusion

PHP turns meta tags from a repetitive chore into an automatic system. Set them as variables for a small site, or pull them from your database for a large one, always escape the output, and add Open Graph tags for clean social sharing. Every page then presents itself to Google and to visitors with a clear, unique and clickable headline — which is exactly what SEO starts with.

Lavkush
Written by

Lavkush Super Admin

Lavkush Kumar is a software developer with over 6 years of experience turning ideas into websites and web applications. He holds a Master of Computer Applications (MCA) degree and enjoys building for the web, exploring new tools, and sharing what he learns along the way.

Through CodingCush, Lavkush creates easy-to-follow tutorials and practical guides to help students and beginners learn software development without feeling overwhelmed. His approach is simple: learn by doing, build real projects, and keep improving one step at a time.

Whether you’re just starting out or looking to sharpen your skills, CodingCush is all about making learning practical, beginner-friendly, and fun.

0 Comments

Leave a Comment