What happens here, you are trying to inject logic into your templates, which is not necessarily wrong, but it creates more confusing and complex code. This problem is associated not only with the <title> tags, but will continue as your pages become more and more dynamic and complex (dynamic navigation, page layouts, etc.).
The MVC approach perfectly solves this problem. MVC consists of models that talk to your data sources (MySQL, Redis, whatever) and process your logic; Views that display HTML and controllers, which are a kind of glue between models and views. Each request that the user makes is ultimately directed to the controller, and then an action method is applied in your controller (for example: /users/login can be displayed on the User controller and the login action).
You must specify the page title (or any other meta-information) in the action method in your controller, which knows that this request, but is called before rendering the view:
$request->setTitle('Home page');
And then, in your opinion, just do it:
<title><?php echo $request->getTitle(); ?></title>
If you are just starting PHP, this is a great time to learn MVC, because it will help you find good habits that affect not only your PHP development, but also any other development.
I recommend you check out CodeIgniter for its simplicity and excellent documentation. I used CodeIgniter for a while, but ended up writing my own frameworks that fit my needs better.
source share